Sort numbers lexicographically: Difference between revisions

Content added Content deleted
m (→‎{{header|AppleScript}}: New sort handler URL and tidy-up in second script.)
Line 171: Line 171:
In the unlikely event of it ever being necessary to sort a ''given'' list of integers in this fashion, one possibility is to create another list containing text versions of the integers and to sort this while rearranging the integer versions in parallel.
In the unlikely event of it ever being necessary to sort a ''given'' list of integers in this fashion, one possibility is to create another list containing text versions of the integers and to sort this while rearranging the integer versions in parallel.


<syntaxhighlight lang="applescript">use sorter : script "Custom Iterative Ternary Merge Sort" -- <https://macscripter.net/viewtopic.php?pid=194430#p194430>
<syntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- Mac OS X 10.9 (Mavericks) or later.
use sorter : script ¬
"Custom Iterative Ternary Merge Sort" -- <www.macscripter.net/t/timsort-and-nigsort/71383/3>


on join(lst, delim)
on sortLexicographically(integerList) -- Sorts integerList in place.
set astid to AppleScript's text item delimiters
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeed
set AppleScript's text item delimiters to delim
set textList to paragraphs of (integerList as text)
set txt to lst as text
set AppleScript's text item delimiters to astid
set AppleScript's text item delimiters to astid
return txt
end join

on sortLexicographically(integerList)
set textList to paragraphs of join(integerList, linefeed)
-- Sort textList, echoing the moves in integerList.
considering hyphens but ignoring numeric strings
considering hyphens but ignoring numeric strings
tell sorter to sort(textList, 1, -1, {slave:{integerList}})
tell sorter to sort(textList, 1, -1, {slave:{integerList}})
Line 185: Line 192:


-- Test code:
-- Test code:
local someIntegers
set someIntegers to {1, 2, -6, 3, 4, 5, -10, 6, 7, 8, 9, 10, 11, 12, 13, -2, -5, -1, -4, -3, 0}
set someIntegers to {1, 2, -6, 3, 4, 5, -10, 6, 7, 8, 9, 10, 11, 12, 13, -2, -5, -1, -4, -3, 0}
sortLexicographically(someIntegers)
sortLexicographically(someIntegers)
return someIntegers
return someIntegers</syntaxhighlight>

--> {-1, -10, -2, -3, -4, -5, -6, 0, 1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9}</syntaxhighlight>
{{output}}
<syntaxhighlight lang="applescript">{-1, -10, -2, -3, -4, -5, -6, 0, 1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9}</syntaxhighlight>


=={{header|Arturo}}==
=={{header|Arturo}}==