Skip to content

Repository files navigation

SortableEnum

MIT License

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.

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]

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)]publicenumJapanese{あいうえお,かきくけこ,さしすせそ,たちつてと}// Culture-aware sorting using Japanese collation rulesvarcultureAscending=SortableEnum.CultureAscending<Japanese>();varcultureDescending=SortableEnum.CultureDescending<Japanese>();

API Reference

Methods

MethodDescriptionReturns
Default<T>()Declaration orderImmutableArray<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 sortingImmutableArray<T>
CultureDescending<T>()Culture-specific descending sortingImmutableArray<T>

Attributes

// 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{ ...}

Error Handling

SortableEnum provides compile-time validation and runtime safety:

Compile-Time Errors

[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 options

Runtime Protection

publicenumUnmarkedEnum{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>();

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>() and CultureDescending<T>() need culture info in attribute

License

This project is licensed under the MIT License.

About

A high-performance C# Source Generator that generates compile-time sorted enum arrays with zero runtime overhead.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages