|
-
Find the second lowest number in an array?
Actionscript 2, Flash 6
I just learned enough about arrays to find the lowest number in a group, and I'm pretty pumped.
But now how do I find the SECOND lowest?
I've found plenty on finding max and mins, but finding the runner up yields an informational desert!
This is what's getting me the lowest so far:
Actionscript Code:
minValue = function (array) { mn = array[0]; for (i=0; i<array.length; i++) { if (array[i]<mn) { mn = array[i]; } } return mn; };
k = new Array(); k = [varible1,variable2,variable3,variable5,etc]; smallest_textbox = (minValue(k)); // and happily, the smallest variable is displayed in the textbox
Any ideas or suggestions on how to think about this problem?
I need the silver medal, #2, runner up!
+Farm
-
Forget that, I'm talking rubbish.. that won't always work.
Last edited by lightspeed10; 03-01-2010 at 07:22 AM.
-
Senior Member
This is just a thought, but maybe looking into the Array.sort(); method. Which would sort the indices based on value, and then select the second index[1], with [0] being the lowest.
Wile E. Coyote - "Clear as mud?"
-
I looked at doing this, but array.sort() will sort the numbers based on the first character of element..
So if you have the array:
var Array = [0,9,7,2,1,500];
Sorting this array would give you:
0,1,2,500,7,9
as the 5 of 500 precedes 7.
-
Senior Member
That's true but one can easily manipulate the function in order to achieve the desired results. A-la-like so,
Actionscript Code:
var nArray:Array = [0, 9, 7, 2, 1, 500]; nArray.sort(Array.NUMERIC); trace(nArray); // returns 0, 1, 2, 7, 9, 500
Wile E. Coyote - "Clear as mud?"
-
Whoa baby! This is so elegant... and it works! Double bonus!
Lightspeed, way to get the ball rolling, much appreciated. And Robb, thanks for the elbow grease!
Best,
Farm
-
Awesome, I've been wondering how to do that!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|