Java中的中文排序(简短版)
在Java中,如果在对一个List或者Map排序,可以采用Collections的集合类中的sort方法来对List进行排序。至于map,可以使用TreeMap自动排序。
但以上排序仅仅是对英文排序时,才会正确,若果数据里面存在中文和英文时,那么排序就乱了。
现在我实现的方法是按照中文的拼音来排序。(网上,还有按笔画排序,在这里我就不一一实现了)
如下是对Map,在新建对象时,给map实现comparator接口。
//为TreeMap增加中文排序
userItems = new TreeMap(new Comparator(){
Collator collator = Collator.getInstance();
public int compare(Object o1, Object o2) {
CollationKey key1 = collator.getCollationKey(o1.toString());
CollationKey key2 = collator.getCollationKey(o2.toString());
return key1.compareTo(key2);
}
});
同样道理,如下是对List
同样也使用了Collections的集合类中的sort方法来对List进行排序,但是重写了comparator的实现方法。
//中文排序,按机构的名称。
Collections.sort(templist, new Comparator(){
Collator collator = Collator.getInstance();
public int compare(Object o1, Object o2) {
TreeBFOEx org1 = (TreeBFOEx)o1;
TreeBFOEx org2 = (TreeBFOEx)o2;
CollationKey key1 = collator.getCollationKey(org1.getDescription());
CollationKey key2 = collator.getCollationKey(org2.getDescription());
return key1.compareTo(key2);
}
});
相关文档:
http://www.ajaxlines.com/ajax/stuff/article/using_google_is_ajax_search_api_with_java.php
I was rather depressed over a year ago when Google deprecated their SOAP Search API with their AJAX Search API. Essentially Google was saying that they didn want anyone programmatically accessing Google search ......
一、Spring基础知识及IOC_选择题
1. 下面关于spring描述错误的是:( )
A Spring支持可插入的事务管理器,使事务划分更轻松,同时无需处理底层的问题。
B Spring事务管理的通用抽象层还包括JTA策略和一个JDBC DataSource。
C 与JTA或EJB CMT一样,Spring的事务支持依赖于Java EE环境。
D Spr ......
1、 Java对象赋值
Java代码
Employee e1=
new
Employee(
"李"
);
//Employee是一个自定义类
Employee e2=e1; //赋值对象
e2.setName("王"
);
//改变对象e2的名字
System.out.println(e1.getName ......