Re: Sorting a list of Lists
Re: Sorting a list of Lists
- Subject: Re: Sorting a list of Lists
- From: Nigel Garvey <email@hidden>
- Date: Fri, 9 Jul 2004 00:56:02 +0100
email@hidden wrote on Thu, 8 Jul 2004 11:05:27 -0400:
>
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.
I'm not sure why you're getting stack overruns with QuickSortOnItem. It
never gives me any problems. Looking at the post in my own archive, I see
there's a splodge where one of the comparison operators should be, so
maybe it's being misinterpreted in your script. (?)
Here's a combination I use myself for a spreadsheet sort. QuickSortOnItem
is slightly improved since I posted it. SortOnColumns uses it to sort and
subsort on selected items of each list in a list, which I think is what
you want. (In my spreadsheet, each sublist represents a row of cells,
which explains the variable names.) The parameters for SortOnColumns are
the list of lists, the indices of the first and last sublist to be
included in the sort, and a list of the items on which to sort. These
handlers don't return a new list but reorder the original.
(* QuickSort handler adapted from Arthur Knapp *)
-- Quicksorts a list of lists, sorting on item n of each list
on QuickSortOnItem(a, top, bottom, n) -- sorts in-place
-- Much thanks to both Serge Belleudy-d'Espinose and Victor Yee
-- for the script-referencing techniques that they helped to
-- refine.
--
script o
property p : a
end script
set i to top
set j to bottom
set v to o's p's item ((top + bottom) div 2)'s item n
repeat while (j > i)
set u to o's p's item i
repeat while (u's item n < v)
set i to i + 1
set u to o's p's item i
end repeat
set w to o's p's item j
repeat while (w's item n > v)
set j to j - 1
set w to o's p's item j
end repeat
if i > j then
else
set o's p's item i to w
set o's p's item j to u
set i to i + 1
set j to j - 1
end if
end repeat
if (top < j) then QuickSortOnItem(o's p, top, j, n)
if (bottom > i) then QuickSortOnItem(o's p, i, bottom, n)
end QuickSortOnItem
on sortOnColumns(theRows, top, bottom, sortColumns)
-- theRows is a list of lists. The lists represent rows in a grid and
their
-- items are the values from the intersections with the corresponding
columns.
-- sortColumns is a list of the column numbers on which successive
sorts and
-- subsorts are to be based.
script o
property rows : theRows
property sortCols : sortColumns
property maxSortLevel : count sortColumns
on soc(top, bottom, sortLevel)
-- Sort this batch of rows on this column
set sortColumn to o's sortCols's item sortLevel
QuickSortOnItem(o's rows, top, bottom, sortColumn)
-- For every run of equal values in this section of this column
-- recurse to subsort on the next specified column
set nextSortLevel to sortLevel + 1
if nextSortLevel does not come after maxSortLevel then
set i to top
repeat with j from top + 1 to bottom
if o's rows's item j's item sortColumn is not o's rows's item
i's item sortColumn then
if j - i > 1 then soc(i, j - 1, nextSortLevel)
set i to j
end if
end repeat
if j > i then soc(i, j, nextSortLevel)
end if
end soc
end script
o's soc(top, bottom, 1)
end sortOnColumns
-- Demo:
set theArray to {{1.59, 4.4, 1.768, 3.586}, {1.31, 2.99, 1.486, 8.004},
{1.035, 2.99, 1.205, 8.004}, {9.59, 2.99, 1.768, 3.586}, {2.31, 4.4,
1.486, 8.004}, {0.035, 2.99, 1.205, 8.004}}
-- Sort on the second item in each list, then subsort on the first
set sortColumns to {2, 1}
-- Sort all the lists (items 1 thru (count theArray) of theArray)
sortOnColumns(theArray, 1, count theArray, sortColumns)
theArray
--> {{0.035, 2.99, 1.205, 8.004}, {1.035, 2.99, 1.205, 8.004}, {1.31,
2.99, 1.486, 8.004}, {9.59, 2.99, 1.768, 3.586}, {1.59, 4.4, 1.768,
3.586}, {2.31, 4.4, 1.486, 8.004}}
NG
_______________________________________________
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.