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.
using SortableEnums;
[Sortable]
public enum Status
{
Active, Inactive, Pending, Archived
}
// All available sorting methods:
var declaration = SortableEnum.Default<Status>();
// [Active, Inactive, Pending, Archived]
var ascending = SortableEnum.Ascending<Status>();
// [Active, Archived, Inactive, Pending]
var descending = SortableEnum.Descending<Status>();
// [Pending, Inactive, Archived, Active]
var caseSensitive = 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)]
public enum Japanese
{
あいうえお, かきくけこ, さしすせそ, たちつてと
}
// Culture-aware sorting using Japanese collation rules
var cultureAscending = SortableEnum.CultureAscending<Japanese>();
var cultureDescending = 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]
public enum MyEnum { ... }
// Culture-specific usage - enables CultureAscending() and CultureDescending() in addition to standard methods
[Sortable("ja-JP", SortableEnumCompareOptions.IgnoreKanaType)]
public enum MyEnum { ... }SortableEnum provides compile-time validation and runtime safety:
[Sortable("invalid-culture", SortableEnumCompareOptions.IgnoreCase)]
public enum BadEnum { A, B }
// Compile error: SortableEnumError001 - Invalid culture name
[Sortable("en-US", (SortableEnumCompareOptions)999)]
public enum BadOptions { A, B }
// Compile error: SortableEnumError002 - Invalid compare optionspublic enum UnmarkedEnum { A, B, C }
// Throws InvalidOperationException at runtime
// "'Ascending' is not available for enum type 'UnmarkedEnum'. The enum must use with 'SortableAttribute'."
var result = 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.