我有一个字符串列表,其中可以包含一个字母或一个 int 的字符串表示形式(最多 2 位数字)。
它们需要按字母顺序排序或(当它实际上是 int 时)按其表示的数值排序。
Example:
IList<string> input = new List<string>()
{"a", 1.ToString(), 2.ToString(), "b", 10.ToString()};
input.OrderBy(s=>s)
// 1
// 10
// 2
// a
// b
我想要的是
// 1
// 2
// 10
// a
// b
我有一些想法涉及格式化它并尝试解析它,然后如果成功尝试解析,则使用我自己的自定义字符串格式化程序对其进行格式化以使其前面有零。我希望有更简单、更高效的东西。
Edit
我最终制作了一个 IComparer,并将其转储到我的 Utils 库中以供以后使用。
当我这样做的时候,我也加入了双打。
public class MixedNumbersAndStringsComparer : IComparer<string> {
public int Compare(string x, string y) {
double xVal, yVal;
if(double.TryParse(x, out xVal) && double.TryParse(y, out yVal))
return xVal.CompareTo(yVal);
else
return string.Compare(x, y);
}
}
//Tested on int vs int, double vs double, int vs double, string vs int, string vs doubl, string vs string.
//Not gonna put those here
[TestMethod]
public void RealWorldTest()
{
List<string> input = new List<string>() { "a", "1", "2,0", "b", "10" };
List<string> expected = new List<string>() { "1", "2,0", "10", "a", "b" };
input.Sort(new MixedNumbersAndStringsComparer());
CollectionAssert.AreEquivalent(expected, input);
}