I have an JavaScript arraylike this:
a[0][0] = "apple";
a[0][1] = 6.5;
a[1][0] = "orange";
a[1][1] = 4.3;
a[2][0] = "pear";
a[2][1] = 3.1;
I want to sort by the float number field in ascending order and assign the content in ascending order as well.
i.e.
a[0][0] = "pear";
a[1][1] = 3.1;
a[1][0] = "orange";
a[1][1] = 4.3;
a[2][0] = "apple";
a[2][1] = 6.5;
I have tried sorted the content but the code seems does not allow float number.
Also, I do not know how to reassign the content in ascending order. Can anyone help me?
a.sort(function(a,b){return a[1] - b[1];});
sort
calls is expected to return<0
ifa
should be beforeb
,>0
ifa
should be afterb
, or0
if they're the same. – T.J. Crowder Feb 15 '11 at 15:51a[i] = []
anda = []
. – Felix Kling Feb 15 '11 at 15:55