要求调用“Array#sort”时,始终提供“compareFunction”。
规则配置
// code-linter.json5
{
"rules": {
"@typescript-eslint/require-array-sort-compare": "error"
}
} 正例
declare const array: string[];
array.sort((a, b) => a.length - b.length);
array.sort((a, b) => a.localeCompare(b));
反例
declare const array: number[];
declare const stringArray: object[];
array.sort();
// String arrays should be sorted using `String#localeCompare`.
stringArray.sort();