https://github.com/sam-parsons/eslint-plugin-explicit-comparator
require comparator argument for Array#sort
https://github.com/sam-parsons/eslint-plugin-explicit-comparator
arrays eslint-plugin sorting
Last synced: about 2 months ago
JSON representation
require comparator argument for Array#sort
- Host: GitHub
- URL: https://github.com/sam-parsons/eslint-plugin-explicit-comparator
- Owner: sam-parsons
- License: mit
- Created: 2021-08-26T14:43:20.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-09-01T00:19:21.000Z (over 3 years ago)
- Last Synced: 2025-02-22T03:18:18.701Z (2 months ago)
- Topics: arrays, eslint-plugin, sorting
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/eslint-plugin-explicit-comparator
- Size: 57.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
eslint-plugin-explicit-comparator
custom rule requiring comparator argument in Array.prototype.sort
---
[![Build Status][build-badge]][build]
[![version][version-badge]][package]
[![MIT License][license-badge]][license]## Rule Details
Using the `sort` method without a comparator argument will coerce all elements in the array to strings and lexigraphically organize them. When sorting an array of numbers or objects, it is advised to provide a comparator function; this plugin enforces that rule.
Examples of **incorrect** code for this rule:
```js
const sorted = [1, 10, 11, 100].sort(); // [1, 10, 100, 11]
```Examples of **correct** code for this rule:
```js
const sorted = [1, 10, 100, 11].sort(function (a, b) {
return a - b;
}); // [1, 10, 11, 100]
```## Installation
This module is distributed via [npm][npm] which is bundled with [node][node] and
should be installed as one of your project's `devDependencies`:```
npm install --save-dev eslint-plugin-explicit-comparator
```This library has a required `peerDependencies` listing for [`eslint`][eslint].
## Usage
Add `explicit-comparator` to the plugins section of your `.eslintrc` configuration file.
You can omit the `eslint-plugin-` prefix:```json
{
"plugins": ["explicit-comparator"]
}
```Then configure the rules you want to use under the rules section.
```json
{
"rules": {
"explicit-comparator/explicit-comparator": "warn"
}
}
```## Further Reading
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description
## LICENSE
MIT
[npm]: https://www.npmjs.com
[node]: https://nodejs.org
[build-badge]: https://img.shields.io/github/workflow/status/sam-parsons/eslint-plugin-explicit-comparator/node-ci?logo=github&style=flat-square
[build]: https://github.com/sam-parsons/eslint-plugin-explicit-comparator/actions?query=workflow%3Anode-ci
[coverage-badge]: https://img.shields.io/codecov/c/github/sam-parsons/eslint-plugin-explicit-comparator.svg?style=flat-square
[version-badge]: https://img.shields.io/npm/v/eslint-plugin-explicit-comparator.svg?style=flat-square
[package]: https://www.npmjs.com/package/eslint-plugin-explicit-comparator
[license-badge]: https://img.shields.io/npm/l/eslint-plugin-explicit-comparator.svg?style=flat-square
[license]: https://github.com/sam-parsons/eslint-plugin-explicit-comparator/blob/main/LICENSE[eslint]: https://eslint.org