NaturalSort.Extension
4.3.0
dotnet add package NaturalSort.Extension --version 4.3.0
NuGet\Install-Package NaturalSort.Extension -Version 4.3.0
<PackageReference Include="NaturalSort.Extension" Version="4.3.0" />
paket add NaturalSort.Extension --version 4.3.0
#r "nuget: NaturalSort.Extension, 4.3.0"
// Install NaturalSort.Extension as a Cake Addin #addin nuget:?package=NaturalSort.Extension&version=4.3.0 // Install NaturalSort.Extension as a Cake Tool #tool nuget:?package=NaturalSort.Extension&version=4.3.0
NaturalSort.Extension
Extension method for StringComparison
or any IComparer<string>
that adds support for natural sorting (e.g. "abc1", "abc2", "abc10" instead of "abc1", "abc10", "abc2").
The library is written in C# and released with an MIT license, so feel free to fork or use commercially.
Any feedback is appreciated, please visit the issues page or send me an e-mail.
Download
Binaries of the last build can be downloaded on the AppVeyor CI page of the project.
The library is also published on NuGet.org, install using:
PM> Install-Package NaturalSort.Extension
<sup>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.</sup>
Usage
The recommended method for best results is to create the comparer by using the StringComparison
enum.
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:
The NaturalSortComparer
created using the extension method is a IComparer<string>
, which you can use in all the places that accept IComparer<string>
(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:
new NaturalSortComparer(StringComparison.OrdinalIgnoreCase)
Note that if you are using a custom IComparer<string>
(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, 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):
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:
SELECT * FROM Customers
ORDER BY Name COLLATE NATURALSORT;
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp1.0 was computed. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard1.3 is compatible. netstandard1.4 was computed. netstandard1.5 was computed. netstandard1.6 was computed. netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
Universal Windows Platform | uap was computed. uap10.0 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 1.3
- NETStandard.Library (>= 1.6.1)
-
.NETStandard 2.0
- No dependencies.
-
net6.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages (12)
Showing the top 5 NuGet packages that depend on NaturalSort.Extension:
Package | Downloads |
---|---|
ImmediaC.SimpleCms
ASP.NET Core based CMS |
|
brianpos.Fhir.R4.WebApi.Support
This library contains the interface definitions that an implementation should use to create the storage layer for a FHIR server using the FHIR WebApi Facade |
|
MutableIdeas.Web.Linq.Query.Service
Package Description |
|
Dox.Services
Package Description |
|
Wororo.Utilities
A set of utilities for my personal projects. |
GitHub repositories (6)
Showing the top 5 popular GitHub repositories that depend on NaturalSort.Extension:
Repository | Stars |
---|---|
icsharpcode/ILSpy
.NET Decompiler with support for PDB generation, ReadyToRun, Metadata (&more) - cross-platform!
|
|
ErsatzTV/ErsatzTV
Stream custom live channels using your own media
|
|
bcssov/IronyModManager
Mod Manager for Paradox Games. Official Discord: https://discord.gg/t9JmY8KFrV
|
|
PlexRipper/PlexRipper
A cross-platform Plex media downloader that seamlessly adds media from other Plex servers to your own!
|
|
pebakery/pebakery
PEBakery is a script engine that specializes in customizing the Windows Preinstalled Environment (WinPE/WinRE).
|
Version | Downloads | Last updated |
---|---|---|
4.3.0 | 156,776 | 4/14/2024 |
4.2.0 | 106,143 | 12/7/2023 |
4.1.0 | 58,567 | 10/6/2023 |
4.0.0 | 227,775 | 9/14/2022 |
3.2.0 | 208,663 | 12/5/2021 |
3.1.0 | 64,899 | 4/30/2021 |
3.0.0 | 145,076 | 5/30/2020 |
2.2.1 | 6,503 | 4/18/2020 |
2.2.0 | 304 | 4/16/2020 |
2.1.0 | 209,166 | 7/10/2019 |
2.0.0 | 3,401 | 5/2/2019 |
1.0.2 | 285,774 | 11/20/2017 |
1.0.1 | 1,513 | 11/17/2017 |
1.0.0 | 2,191 | 11/17/2017 |