I have a piece of code which triggers some actions. Before that it simply sorts weights. Everything works well when I use: weight.sort() where weight is an non-empty array e.g. [10 20 30 40], but when I change that to:
weight.sort(function(a,b) {
return a - b;
});
one of the actions has no effect (display a div). Being honest I don’t know what might cause the difference. It seems strange to me because weight is the same after sorting with both methods and clearly changing that piece of code makes my script working or not. There are no errors in FireBug.
If you do
the items are sorted by using lexicographical comparison.
=> 2 > 10But when you do
the items are compared as numbers instead of string and list sorted in ascending order.
=> 2 < 10Also,
will sort by descending order.