这称为“自然排序顺序”,通常用于对您拥有的项目(如文件名等)进行排序。
这是一个幼稚的(从某种意义上说,它可能存在很多 unicode 问题)实现,似乎可以解决问题:
您可以将下面的代码复制到LINQPad http://www.linqpad.net/执行并测试它。
基本上,比较算法将识别字符串内的数字,并通过用前导零填充最短的数字来处理这些数字,例如两个字符串"Test123Abc"
and "Test7X"
应该像它们一样进行比较"Test123Abc"
and "Test007X"
,这应该会产生你想要的结果。
然而,当我说“天真”时,我的意思是我可能在这里遇到大量真正的 unicode 问题,例如处理变音符号和多代码点字符。如果有人可以提供更好的实现,我很乐意看到它。
Notes:
- 该实现实际上并不解析数字,因此任意长的数字应该可以正常工作
- 由于它实际上并不将数字解析为“数字”,因此浮点数将无法正确处理,“123.45”与“123.789”将被比较为“123.045”与“123.789”,这是错误的。
Code:
void Main()
{
List<string> input = new List<string>
{
"1", "5", "3", "6", "11", "9", "A1", "A0"
};
var output = input.NaturalSort();
output.Dump();
}
public static class Extensions
{
public static IEnumerable<string> NaturalSort(
this IEnumerable<string> collection)
{
return NaturalSort(collection, CultureInfo.CurrentCulture);
}
public static IEnumerable<string> NaturalSort(
this IEnumerable<string> collection, CultureInfo cultureInfo)
{
return collection.OrderBy(s => s, new NaturalComparer(cultureInfo));
}
private class NaturalComparer : IComparer<string>
{
private readonly CultureInfo _CultureInfo;
public NaturalComparer(CultureInfo cultureInfo)
{
_CultureInfo = cultureInfo;
}
public int Compare(string x, string y)
{
// simple cases
if (x == y) // also handles null
return 0;
if (x == null)
return -1;
if (y == null)
return +1;
int ix = 0;
int iy = 0;
while (ix < x.Length && iy < y.Length)
{
if (Char.IsDigit(x[ix]) && Char.IsDigit(y[iy]))
{
// We found numbers, so grab both numbers
int ix1 = ix++;
int iy1 = iy++;
while (ix < x.Length && Char.IsDigit(x[ix]))
ix++;
while (iy < y.Length && Char.IsDigit(y[iy]))
iy++;
string numberFromX = x.Substring(ix1, ix - ix1);
string numberFromY = y.Substring(iy1, iy - iy1);
// Pad them with 0's to have the same length
int maxLength = Math.Max(
numberFromX.Length,
numberFromY.Length);
numberFromX = numberFromX.PadLeft(maxLength, '0');
numberFromY = numberFromY.PadLeft(maxLength, '0');
int comparison = _CultureInfo
.CompareInfo.Compare(numberFromX, numberFromY);
if (comparison != 0)
return comparison;
}
else
{
int comparison = _CultureInfo
.CompareInfo.Compare(x, ix, 1, y, iy, 1);
if (comparison != 0)
return comparison;
ix++;
iy++;
}
}
// we should not be here with no parts left, they're equal
Debug.Assert(ix < x.Length || iy < y.Length);
// we still got parts of x left, y comes first
if (ix < x.Length)
return +1;
// we still got parts of y left, x comes first
return -1;
}
}
}