A high-performance C# Source Generator that generates compile-time sorted enum arrays with zero runtime overhead.
This library is distributed via NuGet
SortableEnum has the following features.
- Zero Runtime Cost: All arrays pre-computed during build
- Type Safe: Full IntelliSense support with source generation
- Culture Support: Built-in culture-aware sorting
- High Performance: Static caching with aggressive inlining
- Error Safe: Compile-time validation and runtime protection
- Target Runtime: .NET Core 3.1 or later is supported.
Tested Runtime Versions:
- .NET Core 3.1
- .NET 5.0, 8.0 and 9.0
By assigning the SortableAttribute to a defined enum, you can get ImmutableArray<TEnum> of sorted enumeration values from the SortableEnum API.
usingSortableEnums;[Sortable]publicenumStatus{Active,Inactive,Pending,Archived}// All available sorting methods:vardeclaration=SortableEnum.Default<Status>();// [Active, Inactive, Pending, Archived]varascending=SortableEnum.Ascending<Status>();// [Active, Archived, Inactive, Pending]vardescending=SortableEnum.Descending<Status>();// [Pending, Inactive, Archived, Active]varcaseSensitive=SortableEnum.AscendingCaseSensitive<Status>();// [Active, Archived, Inactive, Pending]By specifying a culture name and SortableEnumCompareOptions as arguments to SortableAttribute, you can get sort results according to the specified culture and comparison options.
The culture name should be the same as those specified in CultureInfo.GetCultureInfo.
SortableEnumCompareOptions provides the same string comparison options as CompareOptions, and you can basically specify the same values.
[Sortable("ja-JP",SortableEnumCompareOptions.IgnoreCase)]publicenumJapanese{あいうえお,かきくけこ,さしすせそ,たちつてと}// Culture-aware sorting using Japanese collation rulesvarcultureAscending=SortableEnum.CultureAscending<Japanese>();varcultureDescending=SortableEnum.CultureDescending<Japanese>();| Method | Description | Returns |
|---|---|---|
Default<T>() | Declaration order | ImmutableArray<T> |
Ascending<T>() | Alphabetical (case-insensitive) | ImmutableArray<T> |
AscendingCaseSensitive<T>() | Alphabetical (case-sensitive) | ImmutableArray<T> |
Descending<T>() | Reverse alphabetical (case-insensitive) | ImmutableArray<T> |
DescendingCaseSensitive<T>() | Reverse alphabetical (case-sensitive) | ImmutableArray<T> |
CultureAscending<T>() | Culture-specific ascending sorting | ImmutableArray<T> |
CultureDescending<T>() | Culture-specific descending sorting | ImmutableArray<T> |
// Basic usage - enables all standard sorting methods[Sortable]publicenumMyEnum{ ...}// Culture-specific usage - enables CultureAscending() and CultureDescending() in addition to standard methods [Sortable("ja-JP",SortableEnumCompareOptions.IgnoreKanaType)]publicenumMyEnum{ ...}SortableEnum provides compile-time validation and runtime safety:
[Sortable("invalid-culture",SortableEnumCompareOptions.IgnoreCase)]publicenumBadEnum{A,B}// Compile error: SortableEnumError001 - Invalid culture name[Sortable("en-US",(SortableEnumCompareOptions)999)]publicenumBadOptions{A,B}// Compile error: SortableEnumError002 - Invalid compare optionspublicenumUnmarkedEnum{A,B,C}// Throws InvalidOperationException at runtime// "'Ascending' is not available for enum type 'UnmarkedEnum'. The enum must use with 'SortableAttribute'."varresult=SortableEnum.Ascending<UnmarkedEnum>();- Nested enums not supported: Enums defined inside classes are excluded
- Opt-in only: Enums must have
[Sortable]attribute - Culture sorting requires parameters:
CultureAscending<T>()andCultureDescending<T>()need culture info in attribute
This project is licensed under the MIT License.