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.