Skip to content
🔀 Extension method for StringComparer that adds support for natural sorting (e.g. "abc1", "abc2", "abc10" instead of "abc1", "abc10", "abc2").
C#
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
assets Updated 32x32 logo. Nov 20, 2017
src/NaturalSort.Extension
tests Changed the sample console app to .NET 4.7 (AppVeyor issues). Jul 10, 2019
.appveyor.yml
.editorconfig
.gitattributes Initial commit. Nov 17, 2017
.gitignore
LICENSE
NaturalSort.Extension.sln Rewrite of the algorithm with performance in mind. Added sample conso… Jul 10, 2019
NaturalSort.Extension.sln.DotSettings
README.md

README.md

NaturalSort.Extension logo NaturalSort.Extension

Extension method for StringComparer that adds support for natural sorting (e.g. "abc1", "abc2", "abc10" instead of "abc1", "abc10", "abc2").

Build status Tests 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 (prerelease), install using:

PM> Install-Package NaturalSort.Extension

NaturalSort.Extension is built for .NET v4.0, .NET v4.7, .NET Standard 1.3 and .NET Standard 2.0.

Usage

Adds .WithNaturalSort() extension method to any StringComparer you wish to use.

You can use the enhanced comparer in all the places where you can use IComparer<string>, e.g. .OrderBy() or .Sort().

Sample:

var sequence = new[] { "img12.png", "img10.png", "img2.png", "img1.png" };
var ordered = sequence.OrderBy(x => x, StringComparer.OrdinalIgnoreCase.WithNaturalSort());
// ordered will be "img1.png", "img2.png", "img10.png", "img12.png"

For more information about natural sort order, see: Sorting for Humans: Natural Sort Order (Coding Horror).

You can’t perform that action at this time.