SortableEnum 1.0.0
dotnet add package SortableEnum --version 1.0.0
NuGet\Install-Package SortableEnum -Version 1.0.0
<PackageReference Include="SortableEnum" Version="1.0.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="SortableEnum" Version="1.0.0" />
<PackageReference Include="SortableEnum"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add SortableEnum --version 1.0.0
#r "nuget: SortableEnum, 1.0.0"
#:package SortableEnum@1.0.0
#addin nuget:?package=SortableEnum&version=1.0.0
#tool nuget:?package=SortableEnum&version=1.0.0
SortableEnum
A high-performance C# Source Generator that generates compile-time sorted enum arrays with zero runtime overhead.
This library is distributed via NuGet
Features
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
Quick Start
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]
Culture-Specific Sorting
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>();
API Reference
Methods
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> |
Attributes
// 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 { ... }
Error Handling
SortableEnum
provides compile-time validation and runtime safety:
Compile-Time Errors
[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 options
Runtime Protection
public 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>();
Limitations
- 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
License
This project is licensed under the MIT License.
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.0 | 136 | 7/13/2025 |