We look at the default Array.prototype.sort
behavior and discuss how you can do case insensitive string sorting. This isn’t as straightforward as it seems, Javascript by default sorts capitalized words before lowercase. In this lesson we’ll learn exactly how to deal with this behavior using TypeScript.
Can we use
foo.sort((a, b) => a.toLocaleLowerCase(b));
instead of
foo.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase));
?
@Raglb I tried your suggestion in nodejs. It didn't work as expected.
const collator = new Intl.Collator('en', { numeric: true, sensitivity: 'base' }); arrayOfObjects.sort((a, b) => { return collator.compare(a.name, b.name); });