4 ms·
You should have read W3Schools: > However, if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".
by spiderfarmer 11y ago
You should have read W3Schools:
> However, if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".
- xlm1717 11y agoExactly. If even W3Schools describes the default behavior, it's not exactly a secret.
- andreapaiola 11y agoAh ah nope! You should read MDN The default sort order is according to string Unicode code points. If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in Unicode code point order. For example, "Banana" comes before "cherry". In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in Unicode order. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- glitcher 11y agoYet another reason why you shouldn't have read W3 schools.
- Klathmon 11y agoAnd just to throw another wrench in there, because it sorts by Unicode code points, capitals come before lowercase. So this: ['Banana', 'cherry', 'bagel', 'apple'].sort() Will produce this order: ["Banana", "apple", "bagel", "cherry"]