java list 拼音排序,Java List按中文拼音排序

import java.text.Collator;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import java.util.Locale;

import org.apache.log4j.Logger;

/**

* 对List字符串数组进行排序

* @author Administrator

*

*/

public class ComparatorList implements Comparator

{

private static final Logger logger = Logger.getLogger(ComparatorList.class);

public int compare(Object value1, Object value2)

{

//http://j2ee-yohn.javaeye.com/blog/272006  此帖子关于中文拼音排序很详细。

// TODO Auto-generated method stub

if(value1.getClass().getName().equals("org.openjweb.core.util.CodeNameBean"))

{

String s1 = ((CodeNameBean)value1).getName().toString();

String s2 = ((CodeNameBean)value2).getName().toString();

return Collator.getInstance(Locale.CHINESE).compare(s1, s2);

}

else

if(value1.getClass().getName().equals("java.lang.String"))

{

String s1 = value1.toString();

String s2 = value2.toString();

return Collator.getInstance(Locale.CHINESE).compare(s1, s2);

}

return 0; //0表示相同。

}

public static List sort(List strList)

{

ComparatorList comp = new ComparatorList();

Collections.sort(strList,comp);

return strList; //返回排序后的列表

}

}

---

如果现在有model Dictionary,则

import java.text.Collator;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import java.util.Locale;

import com.johnny.model.Dictionary;

/**

* 对字典数据的名字首字母进行排序

* @author JohnnyJiang

*

*/

public class ChineseSort implements Comparator{

/**

* 根据字典名字的首字母进行排序

* @param 要排序的list

* @return 排序后的list

*/

public static Listsort(Listlist){

ChineseSort comp = new ChineseSort();

Collections.sort(list,comp);

return list; //返回排序后的列表

}

public int compare(Dictionary dic1, Dictionary dic2) {

String dicName1 = dic1.getDictionaryName().toString();

String dicName2 = dic2.getDictionaryName().toString();

return Collator.getInstance(Locale.CHINESE).compare(dicName1, dicName2);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值