Reputation: 902
so I use JavaScript and I have an array of variables.
var a=5,b=10,c=3,d=11,e=0; //5 Variables with randomly chosen values
var myArray=[a,b,c,d,e];
myArray.sort(); //sort them, so the lowest value or variable is on first place
alert("This Variable has the lowest value: " + myArray[0]);
//tell me the variable with the lowest value
So basically, i will get this text: "This Variable has the lowest value: 0"
But what i want is: "This Variable has the lowest value: e"
How can i return the variable, instead of the value of the variable?
Best Regards qweret
Upvotes: 4
Views: 7522
Reputation: 1424
as shown here use a sort function as an argument to the sort method call.
for example:
myArray.sort(myArraySortFunction);
function myArraySortFunction(a, b) {
}
you should take a look at javascript associative arrays as described here
Upvotes: 1
Reputation: 35829
You need to make an array of objects:
var array = [{key: 'a', value: 5}, {key: 'b', value: 10}, {key: 'c', value: 3}, {key: 'd', value: 10}, {key: 'e', value: 0}];
Then you apply a sort
function:
array.sort(function(obj1, obj2) {
return obj1.value - obj2.value;
});
Show the alert:
alert("This Variable has the lowest value: " + array[0].key);
See it in action here.
Upvotes: 5