Skip to content

sortStringNaturalAscFn

Sort strings in ascending order using natural (human-friendly) ordering. Numbers embedded in strings are compared numerically: “W2” < “W11” < “W20”.

Available since v2.0.2

import { sortStringNaturalAscFn } from '@helpers4/array';

Natural sort for strings with embedded numbers

Section titled “Natural sort for strings with embedded numbers”

sortStringNaturalAscFn treats numeric parts as numbers: “W2” < “W11” < “W20”.

['W20', 'W2', 'W11', 'W01'].sort(sortStringNaturalAscFn)
// => ['W01', 'W2', 'W11', 'W20']

createSortByNaturalFn sorts objects with embedded numbers in property values.

const items = [{ code: 'W20' }, { code: 'W2' }, { code: 'W11' }, { code: 'W01' }];
items.sort(createSortByNaturalFn('code'))
// => W01, W2, W11, W20

View source on GitHub