ktsu.Sorting
1.1.2
Prefix Reserved
See the version list below for details.
dotnet add package ktsu.Sorting --version 1.1.2
NuGet\Install-Package ktsu.Sorting -Version 1.1.2
<PackageReference Include="ktsu.Sorting" Version="1.1.2" />
<PackageVersion Include="ktsu.Sorting" Version="1.1.2" />
<PackageReference Include="ktsu.Sorting" />
paket add ktsu.Sorting --version 1.1.2
#r "nuget: ktsu.Sorting, 1.1.2"
#:package ktsu.Sorting@1.1.2
#addin nuget:?package=ktsu.Sorting&version=1.1.2
#tool nuget:?package=ktsu.Sorting&version=1.1.2
ktsu.Sorting
A .NET library that provides advanced sorting utilities for specialized sorting scenarios.
Overview
ktsu.Sorting offers a collection of sorting utilities to address common sorting challenges that aren't easily handled by standard sorting methods. The library is designed to be modular and extensible, allowing you to use only the components you need.
Components
NaturalStringComparer
The first component in the ktsu.Sorting library is the NaturalStringComparer, which provides natural string comparison by treating embedded numeric parts as numbers rather than characters.
Problem
Standard string sorting sorts strings character by character, which results in unintuitive ordering for strings containing numbers:
"file1.txt"
"file10.txt"
"file2.txt"
Solution
NaturalStringComparer sorts strings the way a human would expect, recognizing and comparing embedded numbers as numeric values:
"file1.txt"
"file2.txt"
"file10.txt"
Installation
Package Manager
Install-Package ktsu.Sorting
.NET CLI
dotnet add package ktsu.Sorting
Usage
Using NaturalStringComparer
using ktsu.Sorting;
// Create a comparer instance
var comparer = new NaturalStringComparer();
// Compare individual strings
int result = comparer.Compare("file10.txt", "file2.txt"); // Returns > 0 (10 > 2)
// Sort an array using the natural comparer
string[] files = { "file10.txt", "file1.txt", "file100.txt", "file2.txt" };
Array.Sort(files, comparer);
// Result: "file1.txt", "file2.txt", "file10.txt", "file100.txt"
// Use with LINQ
var sortedFiles = files.OrderBy(file => file, new NaturalStringComparer());
Ignoring case
By default text is compared ordinally, so every uppercase letter sorts before every lowercase one. Use NaturalStringComparer.OrdinalIgnoreCase (or new NaturalStringComparer(ignoreCase: true)) to list names the way Windows Explorer and Finder do:
string[] files = { "file10.txt", "File2.txt", "file1.txt", "README.md", "build3.log", "Build20.log" };
Array.Sort(files, NaturalStringComparer.Ordinal);
// "Build20.log", "File2.txt", "README.md", "build3.log", "file1.txt", "file10.txt"
Array.Sort(files, NaturalStringComparer.OrdinalIgnoreCase);
// "build3.log", "Build20.log", "file1.txt", "File2.txt", "file10.txt", "README.md"
Strings that differ only in case are still never equal under OrdinalIgnoreCase. They are ordered next to each other, ordinally, so a SortedSet<string> keeps both file1 and File1.
Letters outside the Basic Multilingual Plane fold too, by code point, so names in Adlam, Deseret or Osage interleave the same way Latin names do. Folded text is still ordered by UTF-16 code unit, as Ordinal orders it, so U+E000 to U+FFFF sort after those letters, where string.Compare(..., StringComparison.OrdinalIgnoreCase) puts them before.
Case variants stay together even when a list also holds a variant that differs only in leading zeros or digit script. The two groupings can't both hold at once, so case wins: each spelling's case variants are adjacent, and the spellings sit side by side:
string[] files = { "photo1.jpg", "photo01.jpg", "Photo1.jpg" };
Array.Sort(files, NaturalStringComparer.OrdinalIgnoreCase);
// "photo01.jpg", "Photo1.jpg", "photo1.jpg"
Features
NaturalStringComparer
- Correctly sorts strings containing numbers in a human-friendly order
- Handles mixed alphanumeric strings
- Properly compares strings with leading zeros
- Fully compatible with .NET's
IComparer<string>interface - Also implements the non-generic
System.Collections.IComparer, for APIs that accept only that:ArrayList.Sort, WPF'sListCollectionView.CustomSort, WinForms'ListView.ListViewItemSorter. Arguments that aren't strings throwArgumentException - Can be used with
Array.Sort(), LINQ'sOrderBy(), and other sorting mechanisms
Examples
NaturalStringComparer Examples
The NaturalStringComparer correctly sorts strings like:
| Input | Standard Sort | Natural Sort |
|---|---|---|
| file1.txt | file1.txt | file1.txt |
| file10.txt | file10.txt | file2.txt |
| file2.txt | file2.txt | file10.txt |
| ------------------------------- | ------------------------------- | |
| z1.doc | z1.doc | z1.doc |
| z10.doc | z10.doc | z2.doc |
| z17.doc | z17.doc | z3.doc |
| z2.doc | z2.doc | z10.doc |
| z23.doc | z23.doc | z17.doc |
| z3.doc | z3.doc | z23.doc |
License
This project is licensed under the MIT License - see the LICENSE.md file for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | 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 | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Memory (>= 4.6.3)
- System.Threading.Tasks.Extensions (>= 4.6.3)
-
.NETStandard 2.1
- No dependencies.
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.1.3-pre.1 | 52 | 11/16/2026 |
| 1.1.2 | 99 | 11/13/2026 |
| 1.1.2-pre.2 | 58 | 11/12/2026 |
| 1.1.2-pre.1 | 55 | 11/11/2026 |
| 1.1.1 | 88 | 11/11/2026 |
| 1.1.0 | 85 | 11/11/2026 |
| 1.0.50-pre.1 | 72 | 10/8/2026 |
| 1.0.49 | 130 | 10/7/2026 |
| 1.0.48 | 109 | 10/5/2026 |
| 1.0.47 | 105 | 9/29/2026 |
| 1.0.47-pre.1 | 73 | 9/28/2026 |
| 1.0.46 | 109 | 9/27/2026 |
| 1.0.45 | 107 | 9/24/2026 |
| 1.0.44 | 117 | 9/23/2026 |
| 1.0.43 | 116 | 9/22/2026 |
| 1.0.42 | 116 | 9/22/2026 |
| 1.0.41 | 113 | 9/21/2026 |
| 1.0.40 | 117 | 9/18/2026 |
| 1.0.39 | 117 | 9/17/2026 |
| 1.0.38 | 117 | 9/16/2026 |
## v1.1.2 (patch)
Changes since v1.1.1:
- Break an ignore-case tie on folded code units, as Ordinal orders them [patch] (#85) (matt-edmondson)