const collator = new Intl.Collator(undefined, { usage: 'sort', numeric: true }) const simple = ['a1', 'a12', 'a2'] simple.sort(collator.compare) // => ['a1', 'a2', 'a12'] const objects = [{"id": "a1"}, {"id": "a12"}, {"id": "a2"}] const by = (key) => (a, b) => collator.compare(a[key], b[key]) objects.sort(by('id')) // => [{id: 'a1'}, {id: 'a2'}, {id: 'a12'}]
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter