|
|
|
|
|
by Too
2833 days ago
|
|
Common mistake when writing a sorting function, a-b is not safe in languages with integer overflow because it will give you incorrect sorting for large numbers. In javascript where everything is double you're probably safe from that kind of error but maybe NaN can give you problems instead? |
|
‘a-b’ is safe enough in JS if you always know the input will be within a safe range regarding integer sizes and types.
NaN can give you all kinds of trouble so I’d usually test for that in a real case, either before the sort or in the comparator—if necessary.