A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Find the second lowest number in an array?

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Location
    Los Angeles
    Posts
    28

    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

  2. #2
    Senior Member
    Join Date
    Jun 2007
    Location
    Nottingham, England
    Posts
    203
    Forget that, I'm talking rubbish.. that won't always work.
    Last edited by lightspeed10; 03-01-2010 at 07:22 AM.

  3. #3
    Senior Member Robb@exo's Avatar
    Join Date
    Jun 2003
    Location
    Canada
    Posts
    1,540
    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?"

  4. #4
    Senior Member
    Join Date
    Jun 2007
    Location
    Nottingham, England
    Posts
    203
    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.

  5. #5
    Senior Member Robb@exo's Avatar
    Join Date
    Jun 2003
    Location
    Canada
    Posts
    1,540
    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?"

  6. #6
    Junior Member
    Join Date
    Feb 2010
    Location
    Los Angeles
    Posts
    28
    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

  7. #7
    Senior Member
    Join Date
    Jun 2007
    Location
    Nottingham, England
    Posts
    203
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center