# sort a list in numerical order

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”}

Thanks,
Nik

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
repeat with i from 1 to (Finish - 1)
if item i of theList > item (i + 1) of theList then
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

So with your example list this is returned:

``{"122334", "12345", "156789", "234567", "23567"}``

You could also use the `sort` program in a shell script:

For example, if the large list of numbers is in a text file:

``/bin/sort <myText.txt``

result:

```122334 12345 156789 234567 23567```
Cheers.

Hi, Hendo;

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
repeat with i from 1 to (Finish - 1)
if item i of B's L > item (i + 1) of B's L then
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
``````

Try something like this:

``````do shell script "/usr/bin/sort --general-numeric-sort"
``````

How do you feed the original list to that, Bruce?

Via stdin, in some form.

``````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"}
``````

Thanks. It was ASCII character 10 that was halting progress.

Ahh – newLine, not return.

Note that `ASCII character 10` can be replaced with `linefeed` in AppleScript 2.0.

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
``````

Thanks,
Nik

easier:

``````
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"

``````

Hi Stefan,
That’s a lot more efficient than my effort,
Thanks very much,
Nik

FYI, if you don’t know how long the list is going to be, you should write to a temporary file instead of using echo.

since this tread was referenced in my thread to do the same thing look at this

http://bbs.applescript.net/viewtopic.php?id=24272