Skip to content

Feature: sorting enums should take into account if it's a numeric based enum #564

@niemyjski

Description

@niemyjski

What rule do you want to change?

https://perfectionist.dev/rules/sort-enums

Describe the problem

Enums get extended over time and they are typically sorted by numeric if they are not string based. For string based, I always want to sort by name.

Code example

export enum SuspensionCode {
    Billing = 0,
    Overage = 1,
    Abuse = 2,
}
export enum SuspensionCode {
    Billing = 0,
    Overage,
    Abuse,
}

Additional comments

I think this might be solved like this:

 'error',
      {
        type: 'unsorted', // disables primary sorting
        sortByValue: true, // sorts using values (will be numeric if possible)
        forceNumericSort: false, // allows fallback when values aren't numeric
        fallbackSort: {
          type: 'alphabetical',
          order: 'asc',
        },
      },

Just kinda feels like this should be the default. I've never seen a numeric enum sorted alpha by default in any language or code project I've used.

Validations

  • Read the docs.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions