Sort using a custom comparator: Difference between revisions

→‎{{header|AppleScript}}: Added two more solutions
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
(→‎{{header|AppleScript}}: Added two more solutions)
Line 264:
 
=={{header|AppleScript}}==
===ASObjC using records===
 
AppleScript is not itself well equipped with sorting functions, but from Yosemite onwards we can make some use of ObjC classes. While a classic comparator function can not readily be passed from AppleScript to ObjC, we can at least write a custom function which lifts atomic values into records (with keys to base and derivative values), and also passes a sequence of (key, bool) pairs, where the bool expresses the choice between ascending and descending order for the paired key:
 
Line 366:
{{Out}}
<pre>{"Sao Paulo", "Shanghai", "Beijing", "Karachi", "Delhi", "Dhaka", "Lagos"}</pre>
 
===ASObjC without records===
 
Putting values into records temporarily can sometimes be necessary with ASObjC sorts so that sorting can be done on the equivalent NSDictionaries' keys. But in fact NSStrings can be sorted on the keys <tt>"length"</tt> and <tt>"self"</tt>:
 
<lang applescript>use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
 
set listOfText to words of "now is the time for all good men to come to the aid of the party"
 
set arrayOfStrings to current application's class "NSMutableArray"'s arrayWithArray:(listOfText)
set descendingByLength to current application's class "NSSortDescriptor"'s sortDescriptorWithKey:("length") ascending:(false)
set ascendingLexicographically to current application's class "NSSortDescriptor"'s sortDescriptorWithKey:("self") ascending:(true) selector:("localizedStandardCompare:")
tell arrayOfStrings to sortUsingDescriptors:({descendingByLength, ascendingLexicographically})
 
return arrayOfStrings as list</lang>
 
{{output}}
<pre>{"party", "come", "good", "time", "aid", "all", "for", "men", "now", "the", "the", "the", "is", "of", "to", "to"}</pre>
 
===Vanilla===
 
While vanilla AppleScript doesn't have sort facilities of its own, a customisable sort written in vanilla, such as [https://macscripter.net/viewtopic.php?pid=194430#p194430 this one on MacScripter], can be fed user-defined comparison handlers to do practically any kind of sorting. The following assumes that the customisable sort just mentioned has been compiled and saved in a suitable "Script Libraries" folder as "Custom Iterative Ternary Merge Sort.scpt":
 
<lang applescript>use AppleScript version "2.3.1" OS X 10.9 (Mavericks) or later
use sorter : script "Custom Iterative Ternary Merge Sort"
 
set listOfText to words of "now is the time for all good men to come to the aid of the party"
 
script descendingByLengthThenDescendingLexicographically
on isGreater(a, b)
set lenA to (count a)
set lenB to (count b)
if (lenA = lenB) then
return (a > b)
else
return (lenB > lenA)
end if
end isGreater
end script
 
-- Sort the whole the list using the above customiser.
tell sorter to sort(listOfText, 1, -1, {comparer:descendingByLengthThenDescendingLexicographically})
return listOfText</lang>
 
{{output}}
<pre>{"party", "come", "good", "time", "aid", "all", "for", "men", "now", "the", "the", "the", "is", "of", "to", "to"}</pre>
 
=={{header|AutoHotkey}}==
557

edits