Hi,
I have a large list of numbers, each list item contains 6 digits. Can anybody let me know how I can sort the list of numbers into a numerical order?
preferably using a shell script!
list example {“156789”,“12345”,“234567”,“23567”,“122334”}
I’m no good with shell scripts but this simple subroutine I put together should do it for you
property SortedAlready : false
property Temp : missing value
property Finish : missing value
set myList to {"156789","12345","234567","23567","122334"} --here is the example list you gave me
Bubble(myList)
return myList
on Bubble(theList)
set Finish to (count of theList) as integer
repeat until SortedAlready or Finish = 1
set SortedAlready to true
repeat with i from 1 to (Finish - 1)
if item i of theList > item (i + 1) of theList then
set SortedAlready to false
set Temp to item i of theList
set item i of theList to item (i + 1) of theList
set item (i + 1) of theList to Temp
end if
end repeat
set Finish to Finish - 1
end repeat
end Bubble
I used a method called the bubblesort, it’s fairly common thing to use to sort stuff
I understood that he wanted the sort in numerical order. Something like this (using your Bubble as a start):
property SortedAlready : false
property Temp : missing value
property Finish : missing value
set theList to {"156789", "12345", "234567", "23567", "122334"}
set ML to Bubble(theList)
--> {12345, 23567, 122334, 156789, 234567}
on Bubble(theList)
script B
property L : theList
end script
set Finish to (count of B's L) as integer
repeat with j from 1 to Finish
set item j of B's L to item j of B's L as integer
end repeat
repeat until SortedAlready or Finish = 1
set SortedAlready to true
repeat with i from 1 to (Finish - 1)
if item i of B's L > item (i + 1) of B's L then
set SortedAlready to false
set Temp to item i of B's L
set item i of B's L to item (i + 1) of B's L
set item (i + 1) of B's L to Temp
end if
end repeat
set Finish to Finish - 1
end repeat
end Bubble
set theList to {"156789", "12345", "234567", "23567", "122334"}
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to ASCII character 10
set theList to "" & theList
set text item delimiters of AppleScript to prevTIDs
do shell script "echo " & quoted form of theList & " | /usr/bin/sort --general-numeric-sort"
set theList to paragraphs of result
--> {"12345", "23567", "122334", "156789", "234567"}
Hi guys,
Thanks very much for your responses. Hendo your code worked very well, Bruce using your shell code is obviously a lot quicker and does the job perfectly. The new list that is created I wanted to put in a text file, which I’ve achieved but can you tell me if there’s a better way of transforming a list into something that I can write to a fileref?
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to ASCII character 10
set list_3 to "" & list_3
set text item delimiters of AppleScript to prevTIDs
do shell script "echo " & quoted form of list_3 & " | /usr/bin/sort"
set list_3 to paragraphs of result
set final_list to {}
repeat with final_item in list_3
set final_list to final_list & final_item & return
end repeat
set final_list to final_list as text
set path_to_desktop to (path to the desktop as string)
try
set fileRef to (open for access (path_to_desktop & "Cleanup.txt") with write permission)
write (final_list) to fileRef
close access fileRef
on error
try
close access fileRef
end try
end try
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to ASCII character 10
set list_3 to "" & list_3
set text item delimiters of AppleScript to prevTIDs
do shell script "echo " & quoted form of list_3 & " | /usr/bin/sort -go ~/Desktop/Cleanup.txt"