Description
I don't remember where I originally found this one, but it's a great little subroutine to do an alphabetical sort on a list! For example, I use this in my Evernote scripts whenever a user needs to pick from a list of their notebooks. By doing a simple sort of the notebook names, I think it's easier for people to find what they're looking for more rapidly. As a tip to newer AppleScripters, here's the way you can use this:- If you have a list of items you'd like to sort, you can cut-and-paste this subroutine to the end of your AppleScript;
- When it's time to sort the list, just send the data through the sorting subroutine. Let's just say our list is contained in a variable called "my_list"... One way to run it through the sorting subroutine would look like this:
set sorted_list to my simple_sort(my_list)
- The result? A new variable named sorted_list is created which has your list items in alphabetical order!
The Code
--SORT SUBROUTINE
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
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