Today I found a strange for me implementation of strings sorting:
['Data', 'Chata', 'Clata'].sort(function(a, b) { return a > b});
Using this approach we take a valid sorted array as the output - ["Chata", "Clata", "Data"]
.
But I don't clearly understand why it works...
I know that comparator function expects three different outputs - zero, integer above zero, integer below zero. But in this case we can take only two values - true (if a greater than b) or false (if a less than b) (1 or 0 after type coercion).
Can anybody explain me why it works?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…