Natural Sort
naturalSort
The naturalSort function compares two strings using a natural sorting order (e.g., "file2" < "file10"). It optionally supports case-insensitive and locale-aware string chunk comparisons.
- Though it compares strings, it was mainly developed for array sorting.
Function Signature
naturalSort(a: string, b: string, options?: SortNature): number;
Parameters
a: The first string to compare.b: The second string to compare.options: Optional settings to configure comparison behavior:caseInsensitive(default:true): Iftrue, compares string chunks without case sensitivity.localeAware(default:false): Iftrue, useslocaleComparefor string chunk comparisons.
Return Value
- A negative number if
acomes beforeb. - A positive number if
acomes afterb. - 0 if the strings are equal.
Aliases
compareNaturally,naturalSortForString,compareSorter
Example Usage
import { naturalSort } from 'nhb-toolbox';
const result = naturalSort("file2", "file10");
console.log(result); // Output: -1
Notes
- The function uses chunking to break strings into numeric and non-numeric parts and compares those parts.
- Supports case-insensitive and locale-aware comparison based on the provided options.
Types
SortNature
interface SortNature {
caseInsensitive?: boolean;
localeAware?: boolean;
}