I have an array with keys like this:
0kefsdfsdf
1101fsdf
55fdsfds
Now I want to sort that array so that the keys start with the smallest number. Afterwards the keys should look like that:
0kefsdfsdf
55fdsf
1101fsdfds
I tried this code, but its not sorting the keys:
myArray.sort(function(a, b) {
return a < b;
});
How can I sort the array according to the keys so that when I iterate the array afterwards, it starts with the key with the lowest number?
.sortcallbacks MUST return: a negative number if a is to be considered less than b, a positive number if a is to be considered greater than b, and zero if they are to be considered equal. You are returning a boolean, which is cast to either 0 or 1 - notably, 1 is returned when a < b, which is the opposite of what you want.{a:1,b:2}- here, a and b are keys. In an array, you have values.