Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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)

Posted on Aug 29, 2013 9:43 AM

Reply
Question marked as Best reply

Posted on Aug 29, 2013 10:32 AM

oops, figured it out.

"set temp to j + 1" should be " set temp to temp + 1"

1 reply

Sorting an Array in Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.