On 7/11/05 1:17 pm, "Tim Welford" <[EMAIL PROTECTED]> wrote:
> Does anybody have experience of using the PRegEx_Sort() function of the
> pregex xtra, I am baffled by what all the arguments mean and how to use
> them, this is probably because it is Monday and I am being a bit thick.
> Can it be used to sort a list of property lists, sorting the outer list
> by the value of a property in each property list contained within.
Hi Tim,
Here's a copy-paste-and-minor-edit of extracts of a recent project where I
used PRegEx_Sort(). Does this help make things clear?
You can make this work with only two movie-level handlers: one which calls
PRegEx_Sort() and one which is called by PRegEx_Sort() each time it wants to
compare two items in a list. My version adds a layer by getting the objects
in the list to sort themselves.
Cheers,
James
============================================================================
-- In a movie script
global gSortOrder -- [#propertySortedAscending: 1,
-- #propertySortedDescending: 0,
-- #anotherSortProperty: <boolean>, ...]
on Data_Sort(aFirstInstance, aSecondInstance) -----------------------
-- SOURCE: Called by the PRegEx xtra when performing the _Sort
-- function
-- INPUT: <aFirstInstance> and <aSecondInstance> will both be
-- Sortable instances
-- OUTPUT: Returns -1, 0 or +1, depending on whether aFirstInstance
-- should appear before, at the same level as, or after
-- aSecondInstance, according to the current sort order.
--------------------------------------------------------------------
return aFirstInstance.Compare(aSecondInstance, gSortOrder)
end Data_Sort
-- In the Sortable parent script
on Compare(me, you, aSortOrder) --------------------------------------
-- SOURCE: Called by Data_Sort() movie handler
-- INPUT: <you> should be another instance of this Sortable script
-- <aSortOrder> should be a property list with the format
-- [<property symbol>: <boolean ascending>, ...]
-- ACTION: Compares <me> with <you>, in terms of the values of the
-- properties listed in aSortOrder
-- OUTPUT: Returns -1 if me is should appear before aSiteRecord in
-- the sort order, +1 if it should appear afterwards, and
-- 0 if the two instances have identical values for all sort
-- properties.
--------------------------------------------------------------------
vCount = aSortOrder.count()
repeat with i = 1 to vCount
vProp = aSortOrder.getPropAt(i)
vMe = me[vProp]
vYou = you[vProp]
if vMe = vYou then
-- We need to check the next item in aSortOrder
else
vAscending = aSortOrder[i] -- 0 | 1
vOutput = (vAscending * 2) - 1 -- -1 | +1
if vMe > vYou then
return vOutput -- +1 if ascending, -1 if not
else
return -vOutput -- -1 if ascending, +1 if not
end if
end if
end repeat
-- If we get here, no differences were found
return 0
end Compare
-- In another script:
on SortListOfInstances(anInputList) ----------------------------------
-- INPUT: <anInputList> should be a list of Sortable instances
-- OUTPUT: Returns a new list containing the items of anInputList
-- in the order defined by gSortOrder
--------------------------------------------------------------------
-- Sort the list of matches
vDeepCopy = FALSE
vSortFunction = #Data_Sort
vOutputList = PRegEx_Sort(anInputList, vDeepCopy, vSortFunction)
return vOutputList
end mData_SortListOfInstances
[To remove yourself from this list, or to change to digest mode, go to
http://www.penworks.com/lingo-l.cgi To post messages to the list, email
[email protected] (Problems, email [EMAIL PROTECTED]). Lingo-L is for
learning and helping with programming Lingo. Thanks!]