Pao*_*sco 11
实施自己的IComparer:
using System;
using System.Collections.Generic;
namespace test {
class Program {
enum X {
one,
two,
three,
four
}
class XCompare : IComparer<X> {
public int Compare(X x, X y) {
// TBA: your criteria here
return x.ToString().Length - y.ToString().Length;
}
}
static void Main(string[] args) {
List<X> xs = new List<X>((X[])Enum.GetValues(typeof(X)));
xs.Sort(new XCompare());
foreach (X x in xs) {
Console.WriteLine(x);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
你可以使用Linq扩展OrderBy,并执行你想要的任何比较魔法:
// order by the length of the value
SomeEnum[] values = (SomeEnum[])Enum.GetValues(typeof(SomeEnum));
IEnumerable<SomeEnum> sorted = values.OrderBy(v => v.ToString().Length);
Run Code Online (Sandbox Code Playgroud)
然后将不同的排序备选方法包装到方法中,并根据用户首选项/输入调用正确的排序方法.
| 归档时间: |
|
| 查看次数: |
10900 次 |
| 最近记录: |