Today I found a strange implementation for sorting strings:
['Data', 'Chata', 'Clata'].sort(function(a, b) { return a > b});
Using this approach, we take a valid sorted array as a result of - ["Chata", "Clata", "Data"]. But I do not understand why this works ...
I know that the comparator function expects three different outputs - zero, an integer above zero, an integer below zero. But in this case, we can take only two values: true (if greater than b) or false (if less than b) (1 or 0 after the forced type).
Can someone explain to me why this works?
source
share