sorting numbers by numerical value, but strings sort the opposite way (i.e. "19999" < "2" ).
In particular, strings are compared character by character from left to right until one or the other characters are different, after which the comparison stops. For example, 19 and 121 will be compared as follows:
"19"[0] != "121"[0] ? // no "19"[1] != "121"[1] ? // yes '9' > '2' ? // yes return some value that indicates "19" greater than "121";
To sort them correctly, you will need to convert them to a numerical value and then sort them. In addition, you can implement your own sorting algorithm that reads numbers correctly.
dsm
source share