-
-
Notifications
You must be signed in to change notification settings - Fork 761
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
Comments
Hot Chocolate introspection returns the values in declaration order, and BCP uses ordering similar to VS Code (by proximity, etc.): Could you please clarify what you meant by "when queried"? |
@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. |
Oh I see. Could you please provide an example, or at least more details? Is this in-memory, EF Core (which provider), etc.? |
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. |
|
|
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:
I don't think there's much that we can do here on the Hot Chocolate side. |
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! |
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.
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.
The text was updated successfully, but these errors were encountered: