I have a map with keys = day of the month, 1
... 31
. If I understand correctly, Javascript converts the map keys into strings anyways.
After sorting using the standard new Map([...myMap.entries()].sort())
the map becomes sorted by the keys as:
1, 11, 12, 13, ..., 19, 2, 20, 21, 22, 23, ... 29, 3, 30, 31.
I found a way to sort as 1,2,3,4,5, ...
by converting the decimal keys to strings and pad single numbers with 0
:
mykey.toString().padStart(2, "0");
I suspect this is too artificial and there should be some pre-built in Javascript method to achieve the normal sort of a map by a decimal values of the keys.
Any suggestions how to sort Javascript map by decimal keys in a nicer way?
Tried new Map([...myMap.entries()].sort()) - the map got sorted as : 1, 11, 12, ..., 19, 2, 20, 21, 22, ... 29, 3, 30, 31.