8000 GitHub - andreasnicolaou/js-natural-sort: Natural sorting of strings and numbers, supporting date, IP, and hex value comparison, with configurable case sensitivity and sorting order.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Natural sorting of strings and numbers, supporting date, IP, and hex value comparison, with configurable case sensitivity and sorting order.

License

Notifications You must be signed in to change notification settings

andreasnicolaou/js-natural-sort

Repository files navigation

js-natural-sort

GitHub package.json version GitHub Actions Workflow Status GitHub License

NPM Downloads

js-natural-sort is a JavaScript package that provides a natural sorting function for strings and numbers, handling various types of data such as dates, IPs, hexadecimal numbers, and regular strings. It offers a more intuitive sorting order compared to default lexicographical sorting.

Features

  • Natural sorting (e.g. '10' < '100')
  • Date-aware comparisons
  • IP address sorting
  • Hexadecimal value support
  • Case-insensitive mode
  • Handles leading zeroes correctly
  • ESM, CommonJS, and AMD build support

Installation

You can install the package via npm:

npm install @andreasnicolaou/js-natural-sort

Usage

import { naturalSort } from '@andreasnicolaou/js-natural-sort';

const arrFloats = ['10.0401', 10.022, 10.042, '10.021999'].sort(naturalSort());
console.log(arrFloats); // ['10.021999', 10.022, '10.0401', 10.042]
import { naturalSort } from '@andreasnicolaou/js-natural-sort';

const arrDates = ['2022-01-02', '2021-12-31', '2020-11-11', '2021-01-01'].sort(naturalSort());
console.log(arrDates); // ['2020-11-11', '2021-01-01', '2021-12-31', '2022-01-02']

Parameters

The function accepts a single optional options object with the following properties:

Parameter Type Description Default
insensitive boolean Whether the sorting should be case-insensitive. false
order 'asc' | 'desc' Sorting order. Can be 'asc' or 'desc'. 'asc'

Contributing

Contributions are welcome! If you encounter issues or have ideas to enhance the library, feel free to submit an issue or pull re 654B quest.

About

Natural sorting of strings and numbers, supporting date, IP, and hex value comparison, with configurable case sensitivity and sorting order.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  
0