Git Product home page Git Product logo

naturalsort.extension's Introduction

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

PM> Install-Package NaturalSort.Extension

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

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: Sorting for Humans: Natural Sort Order (Coding Horror).

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.

naturalsort.extension's People

Contributors

tompazourek avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.