Reputation: 497
Is there any way to actually get the index of the highest value stored in an array?
Like for example
var rate:Array = [10, 15, 12];
how will I know if the highest value in the array resides at index #1?
Upvotes: 0
Views: 690
Reputation: 22604
You need only one loop. Copy the values into an array of objects that know both the index and the value, then use the sortOn method and return the last item in the resulting array.
var arr:Array = [10,15,12];
var temp:Array = [];
arr.forEach( function( item:int, index:int, self:Array ) :void {
temp[temp.length] = {index:index, value: item};
});
var result:Array = temp.sortOn( "value", Array.NUMERIC ); // returns 1
Upvotes: 1
Reputation: 223
There are many sorting algorithms in Data Structure which can help you achieve that. Bubble Sort is the highest among all.
Apart from that you can use the Math function in JS to achieve the same.
var highest = Math.max.apply(Math, values);
// Where 'values' is the array stored on your application.
Upvotes: 1