标签:枚举类型
1.对于数组的排序方法如下:
String[] names = {"王林", "杨宝", "李镇", "刘迪", "刘波"};
Arrays.sort(names, com.ibm.icu.text.Collator.getInstance(com.ibm.icu.util.ULocale.SIMPLIFIED_CHINESE));//升序;
System.out.println(Arrays.toString(names));
2.对于汉字的排序:可以尝试使用ICU4J会得到更好的结果,特别是姓为某些生僻字的时候,
用com.ibm.icu.text.Collator替换java.text.Collator,用com.ibm.icu.util.ULocale替换java.util.Locale
3.对于枚举类型的enum1.compareTo(enum2)是按照枚举类型值在定义时的先后顺序比较的,越后面的越大,而不是按照值的字母先后顺序比较的。
public class Test {
public static void main(String[] args) {
List <Map<String,Object>>totallist = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("PaymentType", PayType.S);
Map<String,Object> map1 = new HashMap<String,Object>();
map1.put("PaymentType", PayType.E);
Map<String,Object> map2 = new HashMap<String,Object>();
map2.put("PaymentType", PayType.Z);
totallist.add(map);
totallist.add(map1);
totallist.add(map2);
Collections.sort(totallist,new Comparator<Map<String,Object>>() {
public int compare(Map<String,Object> arg0, Map <String,Object>arg1) {
PayType t0 =(PayType) arg0.get("PaymentType");
PayType t1 = (PayType)arg1.get("PaymentType");
return t1.compareTo(t0);
}
});
System.out.println(totallist);
}
public enum PayType {
W,
S,
E,
Z;
}
}
打印结果如下:
[{PaymentType=Z}, {PaymentType=E}, {PaymentType=S}]
标签:枚举类型
原文地址:http://8294588.blog.51cto.com/8284588/1738313