Search code examples
actionscript-3flashadobeflash-cc

AS3 - How to find TWO biggest numbers in Array


Was wondering how I could find the TWO biggest numbers in an Array using as3 and Flash. Thanks in advance.


Solution

  • If your array is comprised of just numbers, then you can do the following:

    myArray.sort(Array.NUMERIC);
    
    var biggest:Number = myArray[myArray.length-1];
    var nextBiggest:Number = myArray[myArray.length-2];
    

    Or, sort the other direction:

    myArray.sort(Array.NUMERIC | Array.DESCENDING);
    var biggest:Number = myArray[0];
    var nextBiggest:Number = myArray[1];
    

    If your array is comprised of objects that have a numeric property, you can do the following:

    var myArray:Array = [{name: "Hugh Jass", age: 5},{name: "Homer Simpson", age: 38}];
    
    myArray.sortOn( ["age"], [Array.NUMERIC]);