Re: Sorting a list of Lists
Re: Sorting a list of Lists
- Subject: Re: Sorting a list of Lists
- From: Graff <email@hidden>
- Date: Thu, 08 Jul 2004 13:11:09 -0400
Ok, the best way to do this is to sort the list twice, first sort by
the least important element then sort by the most important element.
The key to the items ending up in the correct order is to use a sorting
routine that preserves the order of elements that have the same value.
In other words, if you have the list {{1, 10},{2, 10}} then sorting by
the second element of each sublist should leave the sorted list as {{1,
10},{2, 10}}. That way your second sort doesn't mix up the order of
the first sort but instead it simply refines it.
Here's an example script that does just this:
----
on BubbleSort(theList, sortIndex)
-- bubble sort a list of sublists by a certain index place in each
sublist
set theSize to length of theList
repeat with i from 1 to theSize
repeat with j from 2 to (theSize - i + 1)
if ((item sortIndex of item (j - 1) of theList) > (item sortIndex
of item j of theList)) then
set temp to (item (j - 1) of theList)
set (item (j - 1) of theList) to (item j of theList)
set (item j of theList) to temp
end if
end repeat
end repeat
return theList
end BubbleSort
set nestedList to {{1.59, 2.99, 1.768, 3.586}, {1.31, 2.99, 1.486,
8.004}, {1.035, 2.99, 1.205, 8.004}}
set firstSort to BubbleSort(nestedList, 2)
set secondSort to BubbleSort(nestedList, 1)
secondSort
--> {{1.035, 2.99, 1.205, 8.004}, {1.31, 2.99, 1.486, 8.004}, {1.59,
2.99, 1.768, 3.586}}
----
- Ken
On Jul 8, 2004, at 11:05 AM, email@hidden wrote:
Ken , thanks for the help.
What I need the sort to do is this. I have a list that contains the
bounds of text boxes on an InDesign page that looks like this.
{{1.59, 2.99, 1.768, 3.586}, {1.31, 2.99, 1.486, 8.004}, {1.035, 2.990,
1.205, 8.004} }
I would like for it to look like this when run through a sort routine.
{{1.035, 2.99, 1.205, 8.004}, {1.31, 2.99, 1.486, 8.004}, {1.59, 2.99,
1.768, 3.586}}
I do not want to arrange the values in the sublists as they contain x,y
coordinates. I would like to be able to sort first based on the second
item in each sublist and then by the first item. When I saw the
quicksortonitem handler in the archives the result was exactly what I
was
looking for but I got stack overruns when running it.
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.