Sort stability: Difference between revisions

Added AppleScript.
m (Fix Perl 6 -> Raku links)
(Added AppleScript.)
Line 24:
 
Also, <code>Vectors</code> and <code>Doubly_Linked_Lists</code> containers have their own internal generic sort. <code>Doubly_Linked_Lists</code> sort is stable.
 
=={{header|AppleScript}}==
 
The AppleScript language doesn't have a built-in sort facility, but there are various possibilities depending on exactly what it is that needs to be sorted:
 
1. A scripted application may possibly have a sort command in its AppleScript dictionary whose invocation causes it to sort its own data internally (by its own criteria) and/or return them to the script in sorted form.
 
2. A sort routine written natively in AppleScript can be used as a library. This will be for sorting AppleScript lists, so if the data to be sorted aren't already in the form of a list, they have to be converted first and possibly converted back afterwards. An example of a stable, customisable AS sort is [https://macscripter.net/viewtopic.php?pid=194430#p194430 here].
 
3. The Foundation framework's NSArray and NSMutableArray classes have sorting methods which appear to be stable. However, as with AppleScript sorts, the data have to be converted first if they're not in the form of one of these classes. Furthermore, the methods usable through AppleScriptObjC only sort on named keys, not on positions, so to sort on the "second column" requires the preparation of an array containing objects which each contain an identifiable key matched with a second-column value.
 
4. If the data are in the form of a single piece of text, AppleScript can pass this to the 'sort' executable which comes with the Bash shell.
 
The task description doesn't specify what form the "table" takes, but here it's assumed to be a tab-delimited text.
 
<lang applescript>set aTable to "UK London
US New York
US Birmingham
UK Birmingham"
 
-- -s = stable sort; -t sets the field separator, -k sets the sort "column" range in field numbers.
set stableSortedOnColumn2 to (do shell script ("sort -st'" & tab & "' -k2,2 <<<" & quoted form of aTable))
set stableSortedOnColumn1 to (do shell script ("sort -st'" & tab & "' -k1,1 <<<" & quoted form of aTable))
return "Stable sorted on column 2:" & (linefeed & stableSortedOnColumn2) & (linefeed & linefeed & ¬
"Stable sorted on column 1:") & (linefeed & stableSortedOnColumn1)</lang>
 
{{output}}
<lang applescript>"Stable sorted on column 2:
US Birmingham
UK Birmingham
UK London
US New York
 
Stable sorted on column 1:
UK London
UK Birmingham
US New York
US Birmingham"
</lang>
 
=={{header|AutoHotkey}}==
557

edits