A C# extension method for StringComparison that enables natural sorting of strings containing numbers.
NaturalSort.Extension is a C# library that provides natural sorting functionality for strings containing numbers. It extends `StringComparison` with a `.WithNaturalSort()` method, allowing developers to sort lists in a human-friendly order where numeric parts are compared as numbers rather than text. This solves the common issue where 'abc10' appears before 'abc2' in standard alphabetical sorts.
.NET developers who need to sort user-facing strings containing numbers, such as filenames, product codes, or version identifiers, in a logical order.
It offers a lightweight, performant solution that integrates seamlessly with existing .NET sorting APIs, supports SQLite collation for database queries, and is available as a signed NuGet package for easy consumption.
🔀 Extension method for StringComparison that adds support for natural sorting (e.g. "abc1", "abc2", "abc10" instead of "abc1", "abc10", "abc2").
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Provides a .WithNaturalSort() extension method that integrates directly with StringComparison, making it effortless to use in LINQ OrderBy clauses without writing custom comparer logic, as shown in the usage examples.
Uses StringComparison directly to avoid substring construction, improving efficiency for sorting operations, which is highlighted in the README as a key optimization to reduce overhead.
Can be registered as a custom collation for natural sorting in SQLite queries, enabling database-level natural ordering without post-processing, as demonstrated in the SQLite usage section.
Implements IComparer<string>, making it compatible with any sorting method that accepts a comparer, such as Array.Sort or List.Sort, offering broad integration with existing .NET code.
Only supports string data types, so sorting objects with numeric properties requires manual extraction to strings first, adding complexity for non-string data scenarios.
Requires explicit code to register the collation on a SQLite connection, as shown in the README, which adds extra steps compared to built-in sorting methods and might be error-prone.
The README doesn't specify handling for all numeric formats, such as negative numbers or decimals with different separators, which could lead to unexpected behavior in some use cases.