3 ms·
Keep going: return (cellA > cellB) * 1 + (cellA < cellB) * -1; More: return Math.sign(cellA - cellB);
by XCSme 7y ago
Keep going:
return (cellA > cellB) * 1 + (cellA < cellB) * -1;
More:
return Math.sign(cellA - cellB);
- XCSme 7y agoYou don't need the * 1 multiplication: return (cellA > cellB) - (cellA < cellB);
- xxs 7y agodoes work for NaN... rookie mistake, eh
- amelius 7y agoMath.sign trick doesn't work for strings.
- XCSme 7y agoThat's true, but the default string comparison sometimes isn't that useful, right? I somehow assumed the data is numbers only, rookie mistake :) 'ana' > 'Bob' '2' > '123' You usually want to clearly define the order or pre-process the strings in some way (trim them, same casing, etc).
- amelius 7y agoWell, you can pass the strings through .toUpperCase() before comparing them. Or indeed drop the <, > based approach and use: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... Or: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...