oxc-project/oxc · info · OxcDiagnostic
Object keys should be sorted
Error message
Object keys should be sorted
What it means
oxlint `eslint/sort-keys`: an object expression's keys are not in ascending order and the object meets the minimum-size threshold, so `sort_properties_diagnostic` (sort_keys.rs:62) reports 'Object keys should be sorted'. Per the rule doc, sorted keys make properties easier to find and diff. The config (shown just above the diagnostic) supports `ignoreCase`, `natural`, `minKeys` (default 2), and `allowLineSeparatedGroups`.
Source
Thrown at crates/oxc_linter/src/rules/eslint/sort_keys.rs:62
impl Default for SortKeysOptions {
fn default() -> Self {
// we follow the eslint defaults
Self {
case_sensitive: true,
natural: false,
min_keys: 2,
allow_line_separated_groups: false,
}
}
}
#[derive(Debug, Default, Clone, JsonSchema, Deserialize)]
#[serde(default)]
pub struct SortKeysConfig(SortOrder, SortKeysOptions);
fn sort_properties_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Object keys should be sorted").with_label(span)
}
declare_oxc_lint!(
/// ### What it does
///
/// When declaring multiple properties, sorting property names alphabetically makes it easier
/// to find and/or diff necessary properties at a later time.
///
/// ### Why is this bad?
///
/// Unsorted property keys can make the code harder to read and maintain.
///
/// ### Examples
///
/// Examples of **incorrect** code for this rule:
/// ```js
/// let myObj = {
/// c: 1,View on GitHub (pinned to e1e7af627c)
Solutions
- Sort the object's keys in ascending order.
- Raise `minKeys` (e.g. 5) so small objects are exempt.
- Set `natural: true` for human-friendly ordering of numbered keys, and `ignoreCase: true` to stop case churn.
- Use `allowLineSeparatedGroups: true` and blank lines to keep semantically distinct groups sorted independently.
Example fix
// before
const config = {
retries: 3,
baseUrl: 'https://api.example.com',
timeout: 1000,
};
// after
const config = {
baseUrl: 'https://api.example.com',
retries: 3,
timeout: 1000,
}; Defensive patterns
Strategy: validation
Prevention
- Tune the config before adopting: `minKeys`, `ignoreCase`, `natural`, and `allowLineSeparatedGroups` exist precisely to cut noise.
- Use blank-line-separated groups for semantically distinct sections and enable `allowLineSeparatedGroups`.
- For maps/config objects where logical order matters, prefer a `Map` or disable the rule for that object.
When it happens
Trigger: `const obj = { b: 1, a: 2 };` — an ObjectExpression with at least `minKeys` properties whose keys descend somewhere instead of ascending. Comparison is lexicographic by default, case-insensitive with `ignoreCase`, natural-order with `natural` (so `item2` < `item10`).
Common situations: Config objects and maps written in logical (not alphabetical) grouping; generated objects; enabling sort-keys on an existing codebase where every multi-key object is a hit.
Related errors
- Expected '{curr_kind}' syntax before '{prev_kind}' syntax.
- Imports should be sorted alphabetically.
- Member '{name}' of the import declaration should be sorted a
- Variable declarations should be sorted
- Unexpected comment inline with code
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/998856a9f8af2d0e.
Report an issue: GitHub.