4 Replies Latest reply on Jan 8, 2009 1:34 PM by ericbelair

# What is the best way to get the minimum or maximum value from an Array of numbers?

Let's say I have an Array of numbers: [2,3,3,4,2,2,5,6,7,2]

What is the best way to find the minimum or maximum value in that Array?

Right now, to get the maximum, I am looping through the Array, and resetting a variable to the value if it is greater than the existing value:
• ###### 2. Re: What is the best way to get the minimum or maximum value from an Array of numbers?
You could use the array to create an ArrayCollection, sort the ac, and then get the last item, though I don't know if this is the most efficient way to do it:
• ###### 3. Re: What is the best way to get the minimum or maximum value from an Array of numbers?
What I ended up doing was sorting the Array (descending for MAX, ascending for MIN) and then grabbing the first item in the Array. Works like a charm in very few lines of code.

Thanks.
• ###### 4. Re: What is the best way to get the minimum or maximum value from an Array of numbers?
I was able to simplify this even more than the original intent.

I actually have an Array of XML objects, and I was stripping out one of the properties from these to get the lowest one.

Here's what I did to make it even more simple: