https://github.com/tompazourek/NaturalSort.Extension
🔀 Extension method for StringComparison that adds support for natural sorting (e.g. "abc1", "abc2", "abc10" instead of "abc1", "abc10", "abc2").
https://github.com/tompazourek/NaturalSort.Extension
c-sharp comparer extension-methods natural-sort naturalsort sort sorting string string-comparison stringcomparer stringcomparison
Last synced: 3 days ago
JSON representation
🔀 Extension method for StringComparison that adds support for natural sorting (e.g. "abc1", "abc2", "abc10" instead of "abc1", "abc10", "abc2").
- Host: GitHub
- URL: https://github.com/tompazourek/NaturalSort.Extension
- Owner: tompazourek
- License: mit
- Created: 2017-11-17T22:19:05.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2025-10-28T15:04:51.000Z (18 days ago)
- Last Synced: 2025-11-11T03:31:38.553Z (5 days ago)
- Topics: c-sharp, comparer, extension-methods, natural-sort, naturalsort, sort, sorting, string, string-comparison, stringcomparer, stringcomparison
- Language: C#
- Homepage:
- Size: 323 KB
- Stars: 200
- Watchers: 4
- Forks: 18
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- anything_about_game - NaturalSort
- awsome-dotnet - NaturalSort.Extension - Extension method for StringComparer that adds support for natural sorting (e.g. "abc1", "abc2", "abc10" instead of "abc1", "abc10", "abc2"). (Misc)
- awesome-csharp - NaturalSort.Extension - Extension method for StringComparer that adds support for natural sorting (e.g. "abc1", "abc2", "abc10" instead of "abc1", "abc10", "abc2"). (Misc)
- awesome-dotnet-cn - NaturalSort.Extension - StringComparer的扩展方法,增加了对自然排序的支持(例:用"abc1", "abc2", "abc10"替代"abc1", "abc10", "abc2"). (杂项)
- fucking-awesome-dotnet - NaturalSort.Extension - Extension method for StringComparer that adds support for natural sorting (e.g. "abc1", "abc2", "abc10" instead of "abc1", "abc10", "abc2"). (Misc / GUI - other)
README
#  NaturalSort.Extension
*Extension method for `StringComparison` or any `IComparer` that adds support for natural sorting (e.g. "abc1", "abc2", "abc10" instead of "abc1", "abc10", "abc2").*
[](https://ci.appveyor.com/project/tompazourek/naturalsort-extension)
[](https://ci.appveyor.com/project/tompazourek/naturalsort-extension/build/tests)
[](https://codecov.io/gh/tompazourek/NaturalSort.Extension)
[](https://www.nuget.org/packages/NaturalSort.Extension/)
[](https://www.nuget.org/packages/NaturalSort.Extension/)
The library is written in C# and released with an [MIT license](https://raw.githubusercontent.com/tompazourek/NaturalSort.Extension/master/LICENSE), so feel **free to fork** or **use commercially**.
**Any feedback is appreciated, please visit the [issues](https://github.com/tompazourek/NaturalSort.Extension/issues?state=open) page or send me an [e-mail](mailto:tom.pazourek@gmail.com).**
Download
--------
Binaries of the last build can be downloaded on the [AppVeyor CI page of the project](https://ci.appveyor.com/project/tompazourek/naturalsort-extension/build/artifacts).
The library is also [published on NuGet.org](https://www.nuget.org/packages/NaturalSort.Extension/), install using:
```
PM> Install-Package NaturalSort.Extension
```
NaturalSort.Extension is built for .NET Standard 1.3, .NET 6, and .NET 8 and is signed to allow use in projects that use strong names.
Usage
-----
The recommended method for best results is to create the comparer by using the `StringComparison` enum.
```csharp
var sequence = new[] { "img12.png", "img10.png", "img2.png", "img1.png" };
var ordered = sequence.OrderBy(x => x, StringComparison.OrdinalIgnoreCase.WithNaturalSort());
// ordered will be "img1.png", "img2.png", "img10.png", "img12.png"
```
For more information about natural sort order, see:
- [Natural sort order (Wikipedia)](https://en.wikipedia.org/wiki/Natural_sort_order)
- [Sorting for Humans: Natural Sort Order (Coding Horror)](https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/)
The `NaturalSortComparer` created using the extension method is a `IComparer`, which you can use in all the places that accept `IComparer` (e.g. `OrderBy`, `Array.Sort`, ...)
If you wish, you can be more explicit by not using the `.WithNaturalSort()` extension method, and using the constructor directly:
```csharp
new NaturalSortComparer(StringComparison.OrdinalIgnoreCase)
```
Note that if you are using a custom `IComparer` (or `StringComparer`), you can also use that instead of the `StringComparison` enum. **However, if you use `StringComparison`, it should perform better as it avoids constructing substrings.**
Usage as collation in SQLite
----------------------------
If you're using [Microsoft.Data.Sqlite](https://docs.microsoft.com/en-us/dotnet/standard/data/sqlite/), it's also possible to use the extension as collation for use in your SQLite queries.
You can register the collation on a SQLite connection as follows (more info in [docs](https://docs.microsoft.com/en-us/dotnet/standard/data/sqlite/collation)):
```csharp
private static readonly NaturalSortComparer NaturalComparer = new(StringComparison.InvariantCultureIgnoreCase);
/* ... */
SqliteConnection conn;
conn.CreateCollation("NATURALSORT", (x, y) => NaturalComparer.Compare(x, y));
```
Then you can use the collation to achieve natural sorting in your SQL query:
```sql
SELECT * FROM Customers
ORDER BY Name COLLATE NATURALSORT;
```