{"record":{"id":"7d7f7575b8ad1d02","repo":"oxc-project/oxc","slug":"use-array-tosorted-instead-of-array-sort","errorCode":null,"errorMessage":"Use `Array#toSorted()` instead of `Array#sort()`.","messagePattern":"Use `Array#toSorted\\(\\)` instead of `Array#sort\\(\\)`\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/unicorn/no_array_sort.rs","lineNumber":22,"sourceCode":"\nuse oxc_ast::{\n    AstKind,\n    ast::{Argument, ArrayExpressionElement, Expression},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nuse crate::{\n    AstNode,\n    ast_util::leftmost_identifier_reference,\n    context::LintContext,\n    rule::{DefaultRuleConfig, Rule},\n    utils::is_import_symbol,\n};\n\nfn no_array_sort_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Use `Array#toSorted()` instead of `Array#sort()`.\")\n        .with_help(\"`Array#sort()` mutates the original array. Use `Array#toSorted()` to return a new sorted array without modifying the original.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Clone, Deserialize, JsonSchema)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\npub struct NoArraySort {\n    /// When set to `true` (default), allows `array.sort()` as an expression statement.\n    /// Set to `false` to forbid `Array#sort()` even if it's an expression statement.\n    ///\n    /// Example of **incorrect** code for this rule with `allowExpressionStatement` set to `false`:\n    /// ```js\n    /// array.sort();\n    /// ```\n    allow_expression_statement: bool,\n\n    /// When set to `true`, allows sorting a fresh array created by a spread, e.g. `[...iterable].sort()`.\n    /// This avoids the double allocation of `toSorted()` when sorting an iterable such as a `Set`.","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/no_array_sort.rs#L4-L40","documentation":"Raised by unicorn/no-array-sort when `Array#sort()` is called (with ES2023 available). Since `sort()` mutates the array in place, the rule directs to the non-mutating variant; the faulting input is the `.sort()` call at the labeled span.","triggerScenarios":"Thrown at crates/oxc_linter/src/rules/unicorn/no_array_sort.rs:22 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Use `array.toSorted()` to get a new sorted array leaving the original untouched","Use `[...array].sort()` on older runtimes"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}