NaturalSort.Extension 4.2.0

This package has a SemVer 2.0.0 package version: 4.2.0+build.231.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package NaturalSort.Extension --version 4.2.0
NuGet\Install-Package NaturalSort.Extension -Version 4.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NaturalSort.Extension" Version="4.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NaturalSort.Extension --version 4.2.0
#r "nuget: NaturalSort.Extension, 4.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NaturalSort.Extension as a Cake Addin
#addin nuget:?package=NaturalSort.Extension&version=4.2.0

// Install NaturalSort.Extension as a Cake Tool
#tool nuget:?package=NaturalSort.Extension&version=4.2.0

NaturalSort.Extension logo 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").

Build status Tests codecov NuGet version NuGet downloads

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 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. 
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 1.3

  • .NETStandard 2.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (11)

Showing the top 5 NuGet packages that depend on NaturalSort.Extension:

Package Downloads
ImmediaC.SimpleCms

ASP.NET Core based CMS

MutableIdeas.Web.Linq.Query.Service

Package Description

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

Dox.Services

Package Description

Wororo.Utilities

A set of utilities for my personal projects.

GitHub repositories (3)

Showing the top 3 popular GitHub repositories that depend on NaturalSort.Extension:

Repository Stars
ErsatzTV/ErsatzTV
Stream custom live channels using your own media
PlexRipper/PlexRipper
The best cross-platform Plex media downloader there is! In active development and feedback is very welcome!
pebakery/pebakery
PEBakery is a script engine that specializes in customizing the Windows Preinstalled Environment (WinPE/WinRE).
Version Downloads Last updated
4.3.0-alpha 66 1/26/2024
4.2.0 17,469 12/7/2023
4.1.0 33,301 10/6/2023
4.0.0 170,343 9/14/2022
3.2.0 173,397 12/5/2021
3.1.0 59,416 4/30/2021
3.0.0 118,163 5/30/2020
2.2.1 5,900 4/18/2020
2.2.0 283 4/16/2020
2.1.0 168,499 7/10/2019
2.0.0 2,685 5/2/2019
1.0.2 280,205 11/20/2017
1.0.1 1,400 11/17/2017
1.0.0 1,657 11/17/2017