You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/eslint-plugin/README.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -160,7 +160,7 @@ Pro Tip: For larger codebases you may want to consider splitting our linting int
160
160
|[`@typescript-eslint/prefer-string-starts-ends-with`](./docs/rules/prefer-string-starts-ends-with.md)| Enforce the use of `String#startsWith` and `String#endsWith` instead of other equivalent methods of checking substrings |:heavy_check_mark:|:wrench:|:thought_balloon:|
161
161
|[`@typescript-eslint/promise-function-async`](./docs/rules/promise-function-async.md)| Requires any function or method that returns a Promise to be marked async |||:thought_balloon:|
162
162
|[`@typescript-eslint/quotes`](./docs/rules/quotes.md)| Enforce the consistent use of either backticks, double, or single quotes ||:wrench:||
163
-
|[`@typescript-eslint/require-array-sort-compare`](./docs/rules/require-array-sort-compare.md)|Enforce giving `compare` argument to `Array#sort`|||:thought_balloon:|
163
+
|[`@typescript-eslint/require-array-sort-compare`](./docs/rules/require-array-sort-compare.md)|Requires `Array#sort`calls to always provide a `compareFunction`|||:thought_balloon:|
164
164
|[`@typescript-eslint/require-await`](./docs/rules/require-await.md)| Disallow async functions which have no `await` expression |:heavy_check_mark:||:thought_balloon:|
165
165
|[`@typescript-eslint/restrict-plus-operands`](./docs/rules/restrict-plus-operands.md)| When adding two variables, operands must both be of type number or of type string |||:thought_balloon:|
166
166
|[`@typescript-eslint/restrict-template-expressions`](./docs/rules/restrict-template-expressions.md)| Enforce template literal expressions to be of string type |||:thought_balloon:|
Copy file name to clipboardExpand all lines: packages/eslint-plugin/docs/rules/require-array-sort-compare.md
+11-9Lines changed: 11 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,23 @@
1
-
# Enforce giving `compare` argument to `Array#sort` (`require-array-sort-compare`)
1
+
# Requires `Array#sort` calls to always provide a `compareFunction` (`require-array-sort-compare`)
2
2
3
-
This rule prevents to invoke`Array#sort()` method without `compare` argument.
3
+
This rule prevents invoking the`Array#sort()` method without providing a`compare` argument.
4
4
5
-
`Array#sort()` method sorts that element by the alphabet order.
5
+
When called without a compare function, `Array#sort()` converts all non-undefined array elements into strings and then compares said strings based off their UTF-16 code units.
6
+
7
+
The result is that elements are sorted alphabetically, regardless of their type.
8
+
When sorting numbers, this results in the classic "10 before 2" order:
This also means that `Array#sort` does not always sort consistently, as elements may have custom `#toString` implementations that are not deterministic; this trap is noted in the noted in the language specification thusly:
12
15
13
16
> NOTE 2: Method calls performed by the `ToString` abstract operations in steps 5 and 7 have the potential to cause `SortCompare` to not behave as a consistent comparison function.<br> > https://www.ecma-international.org/ecma-262/9.0/#sec-sortcompare
14
17
15
18
## Rule Details
16
19
17
-
This rule is aimed at preventing the calls of `Array#sort` method.
18
-
This rule ignores the `sort` methods of user-defined types.
20
+
This rule aims to ensure all calls of the native `Array#sort` method provide a `compareFunction`, while ignoring calls to user-defined `sort` methods.
19
21
20
22
Examples of **incorrect** code for this rule:
21
23
@@ -25,7 +27,7 @@ const stringArray: string[];
25
27
26
28
array.sort();
27
29
28
-
//Even if a string array, warns it in favor of `String#localeCompare` method.
30
+
//String arrays should be sorted using `String#localeCompare`.
29
31
stringArray.sort();
30
32
```
31
33
@@ -41,9 +43,9 @@ array.sort((a, b) => a.localeCompare(b));
0 commit comments