6 Replies Latest reply on Jun 5, 2008 3:21 PM by johnAq

# Min/Max for Property List

Okay, I can easily get the min/max value in a regular list like so:

rList = [514,4567,123,67]
put max(rList)
--4567
put min(rList)
--67

I can even do it for a basic property list:
pList = [#A:514,#B:4567,#C123,#D:67]
put max(pList)
--4567
put min(pList)
--67

The problem comes when I want to have a list of records, and get the max for
a given property in each record, e.g.:
pList = [[#val:514,<other data>],[#val:4567,<other data>],[#val:123,<other
data>],[#val:67,<other data>]]

Now what I want to be able to do is pull the record with the minimum/maximum
for the #val property. The only method I can come up with involves a
tedious repeat loop to search through the entire list, e.g.:

n = pList[1].val
repeat with i = 2 to pList.count
if pList .val > n then n = pList.val
end repeat
put n
--4567

And sure, it works, but my actual lists have a lot more than 4 records, and
this can be a slow process, much slower than the min()/max() functions.
Another method that would be nice is if you could just rip the #val
properties from every record and temporarily put them into their own list,
and then get the min()/max() from that, but I don't know if that's possible
either (without another tedious repeat loop). Is there another way to do
this that I'm overlooking?

• ###### 1. Re: Min/Max for Property List
There are a couple of options

How is you list being compiled? Perhaps you could keep a parallel list running that popoluates in sync with the main one. This seems ineffecient, not unlike your repeat loops.

Another option that will work is this. You currently have a main linear list of property lists. You could actually make this a property list, using the #val from each list as it's property in the main list.

ie
pList = [514:[#val:514,<other data>], 4567:[#val:4567,<other data>], 213:[#val:123,<other
data>], 67:[#val:67,<other data>]]

Now it's a simple matter of sorting this list (prop lists are sorted on the property), and you can then simply access the first or last entry to find min/max

hth

johnAq
• ###### 2. Re: Min/Max for Property List
Hi,

This should work:

pList = [[#val:514,<other data>],[#val:4567,<other data>],[#val:123,<other data>],[#val:67,<other data>]]
pList.sort()
put "min:" && pList[1][#val]
put "max:" && pList[pList.count][#val]

... provided #val is always the first entry in every sub list.

-- Luke
• ###### 3. Re: Min/Max for Property List
> This should work:
>
> pList = [[#val:514,<other data>],[#val:4567,<other data>],[#val:123,<other
> data>],[#val:67,<other data>]]
> pList.sort()
> put "min:" && pList[1][#val]
> put "max:" && pList[pList.count][#val]
>
> ... provided #val is always the first entry in every sub list.

This only works provided the list is sorted - other things depend on the
list being in a specific order, so sorting it is not an option. It might
work if I copied the list into a temporary list like:

tList = pList
tList.sort()
etc.

But as you said, this is assuming #val is the first in every sub list, which
it isn't necessarily. (Each sub list has a number of values, and I may want
to find the min or max for any one of those, not just the first.)

• ###### 4. Re: Min/Max for Property List
> Now it's a simple matter of sorting this list (prop lists are sorted on
> the
> property), and you can then simply access the first or last entry to find
> min/max

Yeah, likewise, see my answer to Lukewig's post. I may need to sort by more
than just one property, plus I can't sort the list, because it has to be in
a defined order.

• ###### 5. Re: Min/Max for Property List
If you copy to a temp list, you'll need to dupliate the list otherwise both variables will point to the same things, given the way Director wirks

tList = pList.duplicate()
tList.sort()

If you need to sort on more than one property, it sounds like you're back to keeping parallel lists

hth

johnAq
• ###### 6. Re: Min/Max for Property List
... or repeat loops

hth

johnAq