Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/gihansoft/naturalstringcomparer

LINQ like method and Comparer<string> that can be used to sort strings by number in them (like what windows explorer do {"text1", "text2", "text10"} instead of {"text1", "text10", "text2"}). pure C# with .NET Standard.
https://github.com/gihansoft/naturalstringcomparer

c-sharp comparer comparison sort sort-strings string

Last synced: about 10 hours ago
JSON representation

LINQ like method and Comparer<string> that can be used to sort strings by number in them (like what windows explorer do {"text1", "text2", "text10"} instead of {"text1", "text10", "text2"}). pure C# with .NET Standard.

Awesome Lists containing this project

README

        

# NaturalStringComparer
[![Build status](https://ci.appveyor.com/api/projects/status/acwd0d2hwng5l5yh?svg=true)](https://ci.appveyor.com/project/chiefmb/naturalstringcomparer)

## install
```sh
dotnet add package GihanSoft.String.NaturalComparer --version 3.5.0
```
or
```pwsh
Install-Package GihanSoft.String.NaturalComparer -Version 3.5.0
```
or
```xml

```

`LINQ` like extension methods and `Comparer` that
can be used to sort strings by number in them.

as what windows explorer do {"text1", "text2", "text10"} instead of {"text1", "text10", "text2"}).

### Features
* **Pure C#**
* **Fast** (because of internal usage of `Span`).
* .NET Standard 1.0
* Cross-Platform
* Optional use of `StringComparison` `enum` as base.

### Usage

```C#
var stringList = new List
{
"number1", "number2", "number3", "number4", "number10", "number15", "number22", "number26"
, "number9", "number33", "number5", "number12"
};

stringList.Sort(new NaturalComparer(StringComparison.Ordinal));
//or
stringList.NaturalSort();

Console.WriteLine("Natural Sort:");
foreach(var item in stringList)
{
Console.WriteLine(item);
}

stringList.Sort();
Console.WriteLine();
Console.WriteLine("Normal Sort:");
foreach(var item in stringList)
{
Console.WriteLine(item);
}

/* output
Natural Sort:
number1
number2
number3
number4
number5
number9
number10
number12
number15
number22
number26
number33

Normal Sort:
number1
number10
number12
number15
number2
number22
number26
number3
number33
number4
number5
number9
*/
```

#### types

```C#
public class NaturalComparer : IComparer
{
}
```

#### extension methods

```C#
// List extension
public static void NaturalSort(
this List src,
Func? keySelector = null,
StringComparison stringComparison = StringComparison.Ordinal);

// T[] extension
public static void NaturalSort(
this TSource[] src,
Func? keySelector = null,
StringComparison stringComparison = StringComparison.Ordinal);

// IEnumerable extensions
public static IOrderedEnumerable NaturalOrderBy(
this IEnumerable source,
Func? keySelector = null,
StringComparison stringComparison = StringComparison.Ordinal);

public static IOrderedEnumerable NaturalOrderByDescending(
this IEnumerable source,
Func? keySelector = null,
StringComparison stringComparison = StringComparison.Ordinal);

public static IOrderedEnumerable NaturalThenBy(
this IOrderedEnumerable source,
Func? keySelector = null,
StringComparison stringComparison = StringComparison.Ordinal);

public static IOrderedEnumerable NaturalThenByDescending(
this IOrderedEnumerable source,
Func? keySelector = null,
StringComparison stringComparison = StringComparison.Ordinal)
```