What's the easiest way to arrange a numbered list from smallest to largest values?IE:{2, 4, 1, 3, 5} becomes {1, 2, 3, 4, 5}or{2, 4, 2, 3, 1} becomes {1, 2, 2, 3, 4}
Any ideas?Thanks in advance,Gary S.
What's the easiest way to arrange a numbered list from smallest to largest values?IE:{2, 4, 1, 3, 5} becomes {1, 2, 3, 4, 5}or{2, 4, 2, 3, 1} becomes {1, 2, 2, 3, 4}
Any ideas?Thanks in advance,Gary S.
I just realized-- “usable for one-digit: integers” otherwise 11 comes before 1…No prob, I invented(?) this little trick after several hundred cups o’ coffee…–example
lset the_list to {33, 27, 4, 4, 100}
copy the_list to {i1, i2, i3, i4, i5}set new_list to {}
repeat 10 times (*Also worked with "repeat 8 times",but I made it "10" just in case.I think the trick to finding the numberof times to repeat is: (Length of list - 1) x 2*)
copy the_list to new_listif item 2 in new_list is less than (item 1 in new_list) then
set new_list to {i2, i1, i3, i4, i5}
end if
if item 3 in new_list is less than (item 2 in new_list) then
set new_list to {i3, i1, i2, i4, i5}
end if
if item 4 in new_list is less than (item 3 in new_list) then
set new_list to {i4, i1, i2, i3, i5}
end if
if item 5 in new_list is less than (item 4 in new_list) then
set new_list to {i5, i1, i2, i3, i4}
end if
copy new_list to the_listset i1 to item 1 in the_listset i2 to item 2 in the_listset i3 to item 3 in the_listset i4 to item 4 in the_listset i5 to item 5 in the_list
end repeat--Lets see if it worked...
display dialog "" & i1 & "" & i2 & "" & i3 & "" & i4 & "" & i5 & ""
What’s the easiest way to arrange a numbered list from smallest to largest values?This is an ASCII sort straight out of the AppleScriptguidebook “Essential subroutines.” It should be usablefor integers or can be adapted for use with any number:
on ASCII_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_indexend
repeat
return the sorted_listend ASCII_Sort
I just realized-- “usable for one-digit integers” otherwise 11 comes before 1…