All Projects → tompazourek → NaturalSort.Extension

tompazourek / NaturalSort.Extension

Licence: MIT license
🔀 Extension method for StringComparison that adds support for natural sorting (e.g. "abc1", "abc2", "abc10" instead of "abc1", "abc10", "abc2").

Programming Languages

C#
18002 projects
powershell
5483 projects

Projects that are alternatives of or similar to NaturalSort.Extension

Data-Structure-Algorithm-Programs
This Repo consists of Data structures and Algorithms
Stars: ✭ 464 (+393.62%)
Mutual labels:  sorting, string, sort
ultra-sort
DSL for SIMD Sorting on AVX2 & AVX512
Stars: ✭ 29 (-69.15%)
Mutual labels:  sorting, sort
spring-boot-jpa-rest-demo-filter-paging-sorting
Spring Boot Data JPA with Filter, Pagination and Sorting
Stars: ✭ 70 (-25.53%)
Mutual labels:  sorting, sort
Queryql
Easily add filtering, sorting, and pagination to your Node.js REST API through your old friend: the query string!
Stars: ✭ 76 (-19.15%)
Mutual labels:  sorting, sort
Table Dragger
Turn your old table to drag-and-drop table with columns and rows sorting like magic!
Stars: ✭ 704 (+648.94%)
Mutual labels:  sorting, sort
algos
A collection of algorithms in rust
Stars: ✭ 16 (-82.98%)
Mutual labels:  sorting, sort
Sorty
Fast Concurrent / Parallel Sorting in Go
Stars: ✭ 74 (-21.28%)
Mutual labels:  sorting, sort
myleetcode
♨️ Detailed Java & Python solution of LeetCode.
Stars: ✭ 34 (-63.83%)
Mutual labels:  string, sort
Quadsort
Quadsort is a stable adaptive merge sort which is faster than quicksort.
Stars: ✭ 1,385 (+1373.4%)
Mutual labels:  sorting, sort
Rummage phoenix
Full Phoenix Support for Rummage. It can be used for searching, sorting and paginating collections in phoenix.
Stars: ✭ 144 (+53.19%)
Mutual labels:  sorting, sort
Rummage ecto
Search, Sort and Pagination for ecto queries
Stars: ✭ 190 (+102.13%)
Mutual labels:  sorting, sort
Sortingalgorithm.hayateshiki
Hayate-Shiki is an improved merge sort algorithm with the goal of "faster than quick sort".
Stars: ✭ 84 (-10.64%)
Mutual labels:  sorting, sort
Cpp Timsort
A C++ implementation of timsort
Stars: ✭ 211 (+124.47%)
Mutual labels:  sorting, sort
PixelGlitch
Image glitch visualization using various Pixel Sorting methods for Processing
Stars: ✭ 25 (-73.4%)
Mutual labels:  sorting, sort
sublime-postcss-sorting
Sublime Text plugin to sort CSS rules content with specified order.
Stars: ✭ 19 (-79.79%)
Mutual labels:  sort
DS ALGO
Data Structures and algorithms
Stars: ✭ 20 (-78.72%)
Mutual labels:  string
repository
[PHP 7] Implementation and definition of a base Repository in Domain land.
Stars: ✭ 26 (-72.34%)
Mutual labels:  sort
ngx-sortable
ngx-sortable is an angular sortable list components that support drag and drop sorting
Stars: ✭ 19 (-79.79%)
Mutual labels:  sorting
lua sort
Lua pure sort algorithm based on lib_table.c (from LuaJIT 2.1.0)
Stars: ✭ 21 (-77.66%)
Mutual labels:  sort
justified
Wrap, align and justify the words in a string.
Stars: ✭ 30 (-68.09%)
Mutual labels:  string

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

NaturalSort.Extension is built for .NET Standard 1.3, and .NET 6 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.

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;
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].