-
Notifications
You must be signed in to change notification settings - Fork 54
Description
Describe the bug
When sorting objects that contain numeric keys, the rule sorts them as strings, not as numbers, resulting in incorrect ordering (e.g., 10 is placed after 1 instead of after 9).
Code example
Input code:
export const MASK_OBJECT = {
1: '999-999-9999',
2: '999 999 9999',
3: '(999) 999-9999',
4: '(999) 999 9999',
5: '(99) 9999-9999',
6: '9999999999',
7: '99999 999 999',
8: '9999 999 9999',
9: '999 9999 9999',
10: '99 99999-9999',
};
Current (Incorrect) Behavior
The rule automatically rearranges the keys as:
export const MASK_OBJECT = {
1: '999-999-9999',
10: '99 99999-9999',
2: '999 999 9999',
3: '(999) 999-9999',
4: '(999) 999 9999',
5: '(99) 9999-9999',
6: '9999999999',
7: '99999 999 999',
8: '9999 999 9999',
9: '999 9999 9999',
};
Expected Behavior
Numeric keys should be sorted in natural numeric order, not lexicographically:
export const MASK_OBJECT = {
1: '999-999-9999',
2: '999 999 9999',
3: '(999) 999-9999',
4: '(999) 999 9999',
5: '(99) 9999-9999',
6: '9999999999',
7: '99999 999 999',
8: '9999 999 9999',
9: '999 9999 9999',
10: '99 99999-9999',
};
Why This Matters
This string-based sorting makes numeric keys confusing and less readable, especially when there are multiple two-digit or higher keys.
It would be great if the rule could:
- Detect purely numeric keys, and
- Sort them using numeric order instead of string order.
ESLint version
v9.38.0
ESLint Plugin Perfectionist version
v4.15.1
Additional comments
Additional Context
This issue becomes more noticeable when working with configuration objects, masks, or enum-like constants using numeric keys.
Validations
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.