Skip to content

Instantly share code, notes, and snippets.

View macmcmeans's full-sized avatar
🎯
Focusing

W. ❝Mac❞ McMeans macmcmeans

🎯
Focusing
View GitHub Profile

Keybase proof

I hereby claim:

  • I am macmcmeans on github.
  • I am macadoo (https://keybase.io/macadoo) on keybase.
  • I have a public key ASAJzICMtgkX2Uh5M26dPtjHmUDLM2dZR3SpTfaAPyhPQgo

To claim this, I am signing this object:

@macmcmeans
macmcmeans / JavaScript-Natural-Sort.txt
Last active March 29, 2022 20:19
JavaScript Natural Sort (strings, integers, decimals, nested arrays)
/*
Perform a natural sort respecting integers, decimals, nested arrays and embedded digits in strings.
This version sorts US currency as decimals.
SO: https://stackoverflow.com/questions/14599321/javascript-natural-sort/71621852#71621852
*/
let items = [1, 0.99, '1.22', 1.24, '3rd', 12, '$1.23', 99, '24th', '99 in the shade', 'B', 'Dec', 400.23, '10000', '101', 'a', [2,3,4], 'Apple', 'apple.sauce', 'Dec3', 'Dec20', 'house11a', 'house11b', 'house', 'house99'];
items.sort( ( a, b ) => ( ( a + '' ).substr( 0, 1 ) === '$' ? ( a + '' ).substr(1, (a + '' ).length ) : ( a + '' ) ).localeCompare( b, 'en', { ignorePunctuation: true, numeric: true, sensitivity: 'base' } ) );