I\'m implementing a sort function and came across the following:
\'49\' > \'5\' // false
\'49\' > \'4\' // true
new String(49).localeCompare(\'4\') // 1
n
That is actually expected behavior when comparing strings, as described here. The easiest thing for this situation would be to convert the values to numbers for comparison if you want to compare them as numbers.
Thinking outside the box a little, you could first compare the length of the strings before using the >
operator. If they are numeric strings, the longer string would have a higher value (assuming you don't have numbers like '0024'). If they are equal in length, the >
operator would then work as you expect.