I have a couple of arrays whose contents depend on each other.
And I need to get them sorted based on the content of one of the arrays.
The tricky thing is that all other arrays need to get in the same new order.
For this I’d need a fast method that outputs a sorted array with the items’ original indices instead of the actual items.
With a method like this:
on sortedIndices(theArray)
...
return theIndices as list
end
. one should get this:
{3,2,1,4,5}
. as result for a line like like this:
sortedIndices({"c","b","a","d","e"})
Unfortunately I don’t have the foggiest notion how to accomplish such a thing the right way (as needs to be fast as well).
Would be the way. I posted the same question 2 weeks ago. ScriptDB can sort. But it needs everything in one multiple list. Here’s their example:
set my_records to {{"George", 45}, {"Beth", 24}, {"Jack", 30}, {"Olivia", 10}, {"Jordan", 15}, {"Sophia", 27}, {"Pikachu", 3}, {"Harry", 13}}
I’ve got mine in multiple lists: like namelist, agelist, heightlist and so on. This is the normal way you get lists, not multiple array as above.
So I was just getting around to doing a index only sort method. Based on sorts handler I got off this site. I will post it to code exchange when I’m finished. You be a great beta tester.
Until then jam into MySQL or FileMaker on the sort (and an less it saved) and get back out. MySQL in faster.
Here’s a mult-list sorter originally suggested by Kai Edwards - easy to extend it to four lists or shorten it to two - it’s shown for three: sortList (arg 1) is the basis for the sort, SecondList, etc. are along for the ride. If the lists are large, it can be speeded up with an internal script object, but I’ve assumed your lists aren’t large enough to bother. Call the handler with the three lists and it returns a list of the sorted lists
to sort3Lists(sortList, SecondList, thirdList)
tell (count sortList) to repeat with i from (it - 1) to 1 by -1
set s to sortList's item i
set r to SecondList's item i
set q to thirdList's item i
repeat with i from (i + 1) to it
tell sortList's item i to if s > it then
set sortList's item (i - 1) to it
set SecondList's item (i - 1) to SecondList's item i
set thirdList's item (i - 1) to thirdList's item i
else
set sortList's item (i - 1) to s
set SecondList's item (i - 1) to r
set thirdList's item (i - 1) to q
exit repeat
end if
end repeat
if it is i and s > sortList's end then
set sortList's item it to s
set SecondList's item it to r
set thirdList's item it to q
end if
end repeat
return {sortList, SecondList, thirdList}
end sort3Lists
The script is an awesome starting point! Wonder why I didn’t find it?
I have adjusted it to allow different amounts of lists dynamically.
sortLists({{"c", "b", "a", "e", "d"}, {"3", "2", "1", "5", "4"}, {3, 2, 1, 5, 4}})
--Result: {{"a", "b", "c", "d", "e"}, {"1", "2", "3", "4", "5"}, {1, 2, 3, 4, 5}}
to sortLists(theLists) -- Will sort the arrays according to the first one
tell (count item 1 of theLists) to repeat with i from (it - 1) to 1 by -1
set cache to {}
repeat with list_number from 1 to count theLists
set end of cache to (item i of ((item list_number of theLists) as list))
end repeat
repeat with i from (i + 1) to it
tell (item i of item 1 of theLists) to if (item 1 of cache) > it then
set item (i - 1) of (item 1 of theLists) to it
repeat with x from 2 to count cache
set item (i - 1) of (item x of theLists) to item i of (item x of theLists)
end repeat
else
set item (i - 1) of (item 1 of theLists) to (item 1 of cache)
repeat with x from 2 to count cache
set item (i - 1) of (item x of theLists) to (item x of cache)
end repeat
exit repeat
end if
end repeat
if it is i and (item 1 of cache) > (end of (item 1 of theLists)) then
repeat with x from 1 to count cache
set item it of (item x of theLists) to (item x of cache)
end repeat
end if
end repeat
theLists
end sortLists