Using Collator for sorting numbers and strings in Javascript

When you want to sort an array of numbers or strings, you can use Intl.Collator.

Collators:

export const sortNumbers = new Intl.Collator(undefined, { numeric: true });
export const sortStrings = new Intl.Collator(undefined, { numeric: false });

Usages:

ipAddresses.sort((aIp, bIp) => sortNumbers .compare(aIp, bIp));
names.sort((aName, bName) => sortStrings .compare(aName, bName));

For more info, check Intl.Collator at MDN.