1 | # `require-array-sort-compare`
|
2 |
|
3 | Requires `Array#sort` calls to always provide a `compareFunction`.
|
4 |
|
5 | This rule prevents invoking the `Array#sort()` method without providing a `compare` argument.
|
6 |
|
7 | 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.
|
8 |
|
9 | The result is that elements are sorted alphabetically, regardless of their type.
|
10 | When sorting numbers, this results in the classic "10 before 2" order:
|
11 |
|
12 | ```ts
|
13 | [1, 2, 3, 10, 20, 30].sort(); //→ [1, 10, 2, 20, 3, 30]
|
14 | ```
|
15 |
|
16 | 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 language specification thusly:
|
17 |
|
18 | :::note
|
19 | 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.
|
20 |
|
21 | https://www.ecma-international.org/ecma-262/9.0/#sec-sortcompare
|
22 | :::
|
23 |
|
24 | ## Rule Details
|
25 |
|
26 | This rule aims to ensure all calls of the native `Array#sort` method provide a `compareFunction`, while ignoring calls to user-defined `sort` methods.
|
27 |
|
28 | Examples of code for this rule:
|
29 |
|
30 |
|
31 |
|
32 | ### ❌ Incorrect
|
33 |
|
34 | ```ts
|
35 | const array: any[];
|
36 | const stringArray: string[];
|
37 |
|
38 | array.sort();
|
39 |
|
40 | // String arrays should be sorted using `String#localeCompare`.
|
41 | stringArray.sort();
|
42 | ```
|
43 |
|
44 | ### ✅ Correct
|
45 |
|
46 | ```ts
|
47 | const array: any[];
|
48 | const userDefinedType: { sort(): void };
|
49 |
|
50 | array.sort((a, b) => a - b);
|
51 | array.sort((a, b) => a.localeCompare(b));
|
52 |
|
53 | userDefinedType.sort();
|
54 | ```
|
55 |
|
56 | ## Options
|
57 |
|
58 | The rule accepts an options object with the following properties:
|
59 |
|
60 | ```ts
|
61 | type Options = {
|
62 | /**
|
63 | * If true, an array which all elements are string is ignored.
|
64 | */
|
65 | ignoreStringArrays?: boolean;
|
66 | };
|
67 |
|
68 | const defaults = {
|
69 | ignoreStringArrays: false,
|
70 | };
|
71 | ```
|
72 |
|
73 | ### `ignoreStringArrays`
|
74 |
|
75 | Examples of code for this rule with `{ ignoreStringArrays: true }`:
|
76 |
|
77 |
|
78 |
|
79 | #### ❌ Incorrect
|
80 |
|
81 | ```ts
|
82 | const one = 1;
|
83 | const two = 2;
|
84 | const three = 3;
|
85 | [one, two, three].sort();
|
86 | ```
|
87 |
|
88 | #### ✅ Correct
|
89 |
|
90 | ```ts
|
91 | const one = '1';
|
92 | const two = '2';
|
93 | const three = '3';
|
94 | [one, two, three].sort();
|
95 | ```
|
96 |
|
97 | ## When Not To Use It
|
98 |
|
99 | If you understand the language specification enough, you can turn this rule off safely.
|
100 |
|
101 | ## Attributes
|
102 |
|
103 | - Configs:
|
104 | - [ ] ✅ Recommended
|
105 | - [ ] 🔒 Strict
|
106 | - [ ] 🔧 Fixable
|
107 | - [x] 💭 Requires type information
|