排序用于UI目的的枚举 [英] sorting enum for UI purpose
本文介绍了排序用于UI目的的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
弓,我们有可能按枚举的int值排序,以枚举名称,并以枚举的翻译名称。
但是,如果我们想要一个不同于上述3的排序怎么办?如何处理这样的要求?
解决方案
实现自己的 IComparer
using System;
使用System.Collections.Generic;
命名空间测试{
类程序{
枚举X {
一,
二,
三,
four
}
class XCompare:IComparer< X> {
public int Compare(X x,X y){
// TBA:你的条件这里
return x.ToString()。Length - y.ToString()。
}
}
static void Main(string [] args){
列表< X> xs = new List< X>((X [])Enum.GetValues(typeof(X)));
xs.Sort(new XCompare());
foreach(x x in xs){
Console.WriteLine(x);
}
}
}
}
Say we have a UI and in this UI we have a dropdown. This dropdown is filled with the translated values of an enum.
Bow, we have the possibility to sort by the int-value of the enum, by the name of the enum, and by the translated name of the enum.
But what if we want a different sorting than the 3 mentioned above. how to handle such a requirement?
解决方案
Implement your own 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);
}
}
}
}
这篇关于排序用于UI目的的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文