soup_or_power@yahoo.com writes:
[color=blue]
> I have an associative array like this:
> arr[x1]=30; arr[x2]=20;arr[x3]=40;arr[x4]=10;[/color]
I guess you have
var arr = new Object();
or something, as well as the variables x1..x4 declared.
[color=blue]
> I want the sort function to sort keys in ascending order of the values
> on the right hand side with the following result:
> x4,x2,x1,x3[/color]
I assume values are always numbers. Otherwise a less trivial
comparison function is needed.
function sortByValue(keyArray, valueMap) {
return keyArray.sort(function(a,b){return valueMap[a]-valueMap[b];});
}
testing:
var arr = {foo: 30, bar: 20, baz:40, doh: 10};
var keyArray = ["foo","bar","baz","doh"]
alert(sortByValue(keyArray, arr));
[color=blue]
> Can anyone please help me write the function?[/color]
There you go.
/L
--
Lasse Reichstein Nielsen -
lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'