Reorder a list

How do I order a list so that it is in alphabetical order?

Any help would be appreciated!

Hi,

take a look at this thread

I used the first handler shown at the top of the thread, however it returns nothing at all.

hi magikseb,

the sorterscript published on macosxautomation.com works fine too, but be careful: it converts every item of a list to a string … (by default …)

set the composer_list to {"Ellington", "Copland", "Bach", "Mozart"}
simple_sort(the composer_list)

on simple_sort(my_list)
    set the index_list to {}
    set the sorted_list to {}
    repeat (the number of items in my_list) times
        set the low_item to ""
        repeat with i from 1 to (number of items in my_list)
            if i is not in the index_list then
                set this_item to item i of my_list as text
                if the low_item is "" then
                    set the low_item to this_item
                    set the low_item_index to i
                else if this_item comes before the low_item then
                    set the low_item to this_item
                    set the low_item_index to i
                end if
            end if
        end repeat
        set the end of sorted_list to the low_item
        set the end of the index_list to the low_item_index
    end repeat
    return the sorted_list
end simple_sort

Thanks both of you! I found that the handler from http://macscripter.net/viewtopic.php?pid=64115#p64115 from the thread you suggested Stefan worked great!

Hans-Gerd the handler you suggested works brilliantly too, is very short and is great for what I need :slight_smile: Problem solved

It’s not meant to return anything, but to sort the list you pass to it:

set yourList to {5, 99, -273, 2000, 4}

Qsort(yourList, 1, (count yourList))
yourList --> {-273, 4, 5, 99, 2000}

hi magikseb,

and never forget about the shell (to get it short) :wink:

set thelist to {"Ellington", "Copland", "Bach", "Mozart", 5}
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\n"
set thelist to paragraphs of (do shell script "echo " & quoted form of (thelist as text) & " | sort" & "  -f -b") --have a look for the options in the man-page ...
set AppleScript's text item delimiters to tid

Hans

Thanks again guys!

@Nigel I didn’t realize that! :S
@Hans-Gerd That works and it’s super short! :slight_smile:

The shell sort command is useful, but it’s very limited. It doesn’t handle accented or non-ASCII characters properly, and it’s order even for things like punctuation might not be what you expect (it sorts in simple ASCII-number order).