Sorting an Array in Applescript
Hi
I'm having two arrays, the first is messy, the second is sorted after its value.
set number_list to {20, 30, 20, 40}
set sorted_numbers to {20, 20, 30, 40}
Now i would like to compare the two arrays and how the items in the array have been moved.
I translated following return method directly from java, which works fine in java, but doesnt in applescript.
on compareLists(my_list, sortedList)
set len to (the number of items in my_list)
set comparedList to {}
repeat with i from 1 to len
set end of comparedList to -1
end repeat
repeat with i from 1 to len
repeat with j from 1 to len
if (item i of my_list) > (item j of sortedList) then
set temp to j
repeat with k from 1 to len
if temp = item (k) of comparedList then
set temp to j + 1
end if
end repeat
set item i of comparedList to (temp)
--exit repeat
end if
end repeat
end repeat
return comparedList
end compareLists
Calling this method:
return compareLists(number_list, sorted_numbers)
... doesnt produce the ouput as expected:
{1, 3, 2, 4
}
instead you it returns:
{-1, 2, -1, 4}
I also asked this question on Stackoverflow. For more informations please visit http://stackoverflow.com/questions/18515013/sorting-an-array-in-applescript
Thanks!
MacBook Pro (15-inch Early 2008), Mac OS X (10.7.4)