ID:153011
 
I am just wondering what is the most effecient way to pick the lowest and highest numbers from a list? For example, I have four numbers linked to four datums, and I need to select whichever datum has the lowest number. What would be the most effective way to do this?


~Polatrite~
Do you mean the lowest and highest indices of the list (so the first and last items in it), or a var belonging to the datum?

Getting the first and last items in a list is really straightforward... L[1] and L[length(L)] respectively. For the latter (a var belonging to the datums), you have to loop through the whole list and test the variable on each datum:

var/minimum_datum=null
for (var/D in somelistofdatums)
if (minimum_datum==null || D.somevar < minimum_datum.somevar)
minimum_datum = D


On a slightly unrelated note... I looked at the reference entry for min() while writing this post, and realised you can also pass an actual /list to it instead of just a series of numbers (i.e. min(list(1,2,3)) is equivalent to min(1,2,3)). Me likes. =)
I am just wondering what is the most effecient way to pick the lowest and highest numbers from a list? For example, I have four numbers linked to four datums, and I need to select whichever datum has the lowest number. What would be the most effective way to do this?

If your list is unsorted you unfortunantly need to check every item in the list. If you're looking for the min/max a lot more than you are adding items to the list you might just want to keep the list sorted. Then picking the min/max becomes trivial.
In response to Crispy
Crispy wrote:
On a slightly unrelated note... I looked at the reference entry for min() while writing this post, and realised you can also pass an actual /list to it instead of just a series of numbers (i.e. min(list(1,2,3)) is equivalent to min(1,2,3)). Me likes. =)

Man, I checked the reference for at least 15 minutes looking for something. How did I miss this? Just what I needed!