Created
October 27, 2015 03:20
-
-
Save LGM-AdrianHum/970f34a7ddd49aa650d1 to your computer and use it in GitHub Desktop.
Allows you to sort an array of strings in a natural order where the numeric component of the string is of random length.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main(){ | |
new[] {"a4","a3","a2","a10","b5","b4","b400","1","C1d","c1d2"}.OrderBy(x => x, new NaturalStringComparer()).Dump(); | |
} | |
public class NaturalStringComparer : IComparer<string>{ | |
private static readonly Regex _re = new Regex(@"(?<=\D)(?=\d)|(?<=\d)(?=\D)", RegexOptions.Compiled); | |
public int Compare(string x, string y) | |
{ | |
x = x.ToLower(); | |
y = y.ToLower(); | |
if(string.Compare(x, 0, y, 0, Math.Min(x.Length, y.Length)) == 0) | |
{ | |
if(x.Length == y.Length) return 0; | |
return x.Length < y.Length ? -1 : 1; | |
} | |
var a = _re.Split(x); | |
var b = _re.Split(y); | |
int i = 0; | |
while(true) | |
{ | |
int r = PartCompare(a[i], b[i]); | |
if(r != 0) return r; | |
++i; | |
} | |
} | |
private static int PartCompare(string x, string y) | |
{ | |
int a, b; | |
if(int.TryParse(x, out a) && int.TryParse(y, out b)) | |
return a.CompareTo(b); | |
return x.CompareTo(y); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment