Skip to content

linter: unicorn/no-array-sort reports non-array .sort() calls #22487

@ardalanamini

Description

@ardalanamini

What version of Oxlint are you using?

1.65.0

What command did you run?

oxlint -c ./oxlint.config.ts --type-aware .

What does your .oxlintrc.json (or oxlint.config.ts) config file look like?

{
  "categories": {
    "correctness": "off"
  },
  "plugins": ["unicorn"],
  "rules": {
    "unicorn/no-array-sort": "error",
  }
}

What happened?

Description

unicorn/no-array-sort reports false positives for non-array .sort() methods, specifically Mongoose query sorting.

The rule is intended to discourage mutating Array.prototype.sort() and suggest toSorted() instead. However, Mongoose’s .sort() is not Array#sort; it is a query builder method used to specify database sort order.

Reproduction

import mongoose from "mongoose";

const userSchema = new mongoose.Schema({
  name: String,
  createdAt: Date,
});

const User = mongoose.model("User", userSchema);

async function getUsers() {
  return User.find().sort({ createdAt: -1 });
}

Actual behavior

Oxlint reports unicorn/no-array-sort on the Mongoose .sort() call:

User.find().sort({ createdAt: -1 });
            ^^^^

It suggests avoiding Array#sort() / using toSorted().

Expected behavior

The rule should not report this code, because User.find().sort(...) is not Array.prototype.sort().

This .sort() call belongs to Mongoose’s query API and does not mutate a JavaScript array.

Why this matters

In Node.js/TypeScript backend projects, .sort() is commonly used by database query builders and ORMs/ODMs, for example:

User.find().sort({ createdAt: -1 });
User.find().sort("-createdAt");
Post.find({ published: true }).sort({ updatedAt: "desc" });

Flagging these as unicorn/no-array-sort creates noisy false positives and makes the rule difficult to enable in projects using Mongoose.

Expected scope of the rule

This should still be reported:

const items = [3, 1, 2];
items.sort();

But this should not be reported:

User.find().sort({ createdAt: -1 });

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Priority

    None yet

    Effort

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions