Skip to main content

sort-array

Enforce alphabetical sorting for literal array elements.

Targeted pattern scope

This rule checks ArrayExpression nodes and only analyzes arrays where every non-empty element is a literal value (Literal).

If any element is non-literal (for example identifier, function call, template expression, or spread), the array is skipped.

What this rule reports

This rule reports array literals whose sortable elements are not in ascending alphabetical order (using localeCompare on each literal coerced to string).

The rule provides an autofix that reorders the sortable elements.

Why this rule exists

Canonical ordering in static lists reduces merge conflicts and makes lookups faster during code review.

❌ Incorrect

const statuses = [
"pending",
"active",
"archived",
];

✅ Correct

const statuses = [
"active",
"archived",
"pending",
];

Behavior and migration notes

This rule is autofixable and safe for static literal arrays.

Important caveat: literal values are sorted as strings. Numeric arrays are therefore ordered lexicographically (10 before 2).

Options

This rule has no options.

Additional examples

const labels = ["beta", "alpha"];
// ❌ reported and autofixed

const dynamic = [prefix, "alpha"];
// ✅ ignored because not all elements are literals

ESLint flat config example

import etcMisc from "eslint-plugin-etc-misc";

export default [
{
plugins: { "etc-misc": etcMisc },
rules: {
"etc-misc/sort-array": "error",
},
},
];

When not to use it

Disable this rule when ordering carries semantic meaning (for example, priority or display order), or when you need locale-specific/custom sorting semantics.

Package documentation

Rule catalog ID: R065

Further reading

Adoption resources

  • Start at warning level in CI, then move to error after cleanup.
  • Use focused codemods/autofix batches per package or directory.