这称为“自然排序顺序”,通常用于对您拥有的项目进行排序,例如文件名等。
这是一个似乎可以解决问题的幼稚实现(从某种意义上说,它可能存在很多 unicode 问题):
您可以将下面的代码复制到LINQPad中执行并测试。
基本上,比较算法将识别字符串中的数字,并通过用前导零填充最短的数字来处理这些数字,因此例如应该比较两个字符串 "Test123Abc" 和 "Test7X",就好像它们是 "Test123Abc" 和"Test007X",应该会产生你想要的。
但是,当我说“天真”时,我的意思是我可能在这里遇到了大量真正的 unicode 问题,比如处理变音符号和多代码点字符。如果有人可以提供更好的实现,我很乐意看到它。
注意事项:
- 该实现实际上并不解析数字,因此任意长的数字应该可以正常工作
- 由于它实际上并没有将数字解析为“数字”,因此无法正确处理浮点数,“123.45”与“123.789”将被比较为“123.045”与“123.789”,这是错误的.
代码:
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;
}
}
}