1. 程式人生 > >java實現按拼音排序

java實現按拼音排序

List<WaPayFileVO> list =(List<WaPayFileVO>) dao.execQueryBeanList(pagesql, params.toArray(),  new BeanListProcessor(WaPayFileVO.class));

 if("asc".equals(page.getOrder()) && "pk_psnjob__pk_psndoc__name".equals(page.getSort())){
            Collections.sort(list, new Comparator<WaPayFileVO>() {
                @Override
                
public int compare(WaPayFileVO o1, WaPayFileVO o2) { return Collator.getInstance(Locale.CHINESE).compare(o1.getPk_psnjob__pk_psndoc__name(),o2.getPk_psnjob__pk_psndoc__name()); } }); }else if("desc".equals(page.getOrder()) && "pk_psnjob__pk_psndoc__name".equals(page.getSort())){ Collections.sort(list,
new Comparator<WaPayFileVO>() { @Override public int compare(WaPayFileVO o1, WaPayFileVO o2) { return -Collator.getInstance(Locale.CHINESE).compare(o1.getPk_psnjob__pk_psndoc__name(),o2.getPk_psnjob__pk_psndoc__name()); } }); }

jdk自帶的Collator包涵的漢字太少了,對一些生僻的姓氏不能進行排序。推薦使用:

import com.ibm.icu.text.Collator; 

是ibm開發的針對編碼的工具包,非常好用。附pom檔案:

<dependency>  
            <groupId>com.ibm.icu</groupId>  
            <artifactId>icu4j</artifactId>  
            <version>57.1</version>  
        </dependency>