Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting enums by their values #7099

Closed
PHILLIPS71 opened this issue May 12, 2024 · 8 comments
Closed

Sorting enums by their values #7099

PHILLIPS71 opened this issue May 12, 2024 · 8 comments
Labels
🌶️ hot chocolate Next Next up on the backlog.
Milestone

Comments

@PHILLIPS71
Copy link
Contributor

PHILLIPS71 commented May 12, 2024

Product

Hot Chocolate

Is your feature request related to a problem?

Sorting enums based on their values appears to be unattainable at present. Instead, they are sorted by their names, which may suit certain scenarios but can be cumbersome for others.

The solution you'd like

Given the example below, I aim to have the statuses sorted based on their values. Hence, when queried, I anticipate seeing entries in the order of Open, Pending, and Closed.

public enum Status
{
    Open = 0,
    Pending = 1,
    Closed = 2
}

Currently, enums are sorted alphabetically by their names. Consequently, the resulting sort order for entries would be Closed, Open, Pending, which is not preferred in this context.

@glen-84 glen-84 changed the title sorting enums by their values Sorting enums by their values May 12, 2024
@michaelstaib michaelstaib added this to the HC-14.0.0 milestone May 13, 2024
@michaelstaib michaelstaib added the Next Next up on the backlog. label May 13, 2024
@glen-84
Copy link
Collaborator

glen-84 commented Jun 6, 2024

@PHILLIPS71

Hot Chocolate introspection returns the values in declaration order, and BCP uses ordering similar to VS Code (by proximity, etc.):

image

Could you please clarify what you meant by "when queried"?

@PHILLIPS71
Copy link
Contributor Author

PHILLIPS71 commented Jun 7, 2024

@glen-84 thanks for looking into this!

I'm referring to the scenario where you query a collection and apply a sort order to an enum field. It would be useful if there was a way to sort the collection by the enum's numeric values (0, 1, 2, etc.) instead of sorting by the enum's names, which is the current behavior.

@glen-84
Copy link
Collaborator

glen-84 commented Jun 7, 2024

Oh I see.

Could you please provide an example, or at least more details? Is this in-memory, EF Core (which provider), etc.?

@PHILLIPS71
Copy link
Contributor Author

PHILLIPS71 commented Jun 8, 2024

I'm currently using EF Core, though I think sorting by enum values should be provider agnostic?

However, I'm uncertain about the approach to achieve this. It could potentially be seen as a disruptive change since some users may prefer sorting by enum names. I'd assume this would be something you'd opt-into, perhaps at the type level, allowing certain types to opt for value-based enum sorting over name-based sorting.

@glen-84
Copy link
Collaborator

glen-84 commented Jun 11, 2024

  1. Which provider are you using?
  2. Is the enum mapped to a string type or an enum type in the database?

@PHILLIPS71
Copy link
Contributor Author

PHILLIPS71 commented Jun 13, 2024

  1. Which provider are you using?
  2. Is the enum mapped to a string type or an enum type in the database?
  1. postgres
  2. a string usually, though there are cases an int is used

@glen-84
Copy link
Collaborator

glen-84 commented Jun 15, 2024

Unless I'm missing something, the ordering is simply going to follow the data type of the column – if it's a string, then it'll sort by name, and if it's an integer, then it'll sort by value.

In other words, if you want status to be sorted by value, then consider storing it as an integer.

The only other options that I can think of:

  1. Custom SQL that does something like:
    • ORDER BY CASE status WHEN "Open" THEN 0 WHEN "Pending" THEN 1 WHEN "Closed" THEN 2 END
      • (possibly not great for performance)
  2. Mapping to a PostgreSQL enum, in which case the order is based on the order in which the values are listed when the type is created.

I don't think there's much that we can do here on the Hot Chocolate side.

@PHILLIPS71
Copy link
Contributor Author

I realize now that I've misunderstood the sorting process. I initially thought it would sort based on the enum value after conversion. However, since the conversion occurs only after the result set is returned, the sorting is already completed by that point. I'll close this issue since it was a misunderstanding on my part and use one of the options you've provided. Thanks for clearing this up, I appreciate it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌶️ hot chocolate Next Next up on the backlog.
Projects
None yet
Development

No branches or pull requests

3 participants