比这篇新的文章:
Codee#2641
比这篇旧的文章: Codee#2639
作者: 云之麒, 点击114次, 评论(0), 收藏者(0), , 打分:
所有评论,共0条:( 我也来说两句)
比这篇旧的文章: Codee#2639
Codee#2640
语言: Java, 标签: 无 2009/07/04发布 8个月前更新作者: 云之麒, 点击114次, 评论(0), 收藏者(0), , 打分:
Java语言: Codee#2640
01 import java.text.CollationKey;
02 import java.text.Collator;
03 import java.text.RuleBasedCollator;
04 import java.util.Comparator;
05
06 /**
07 * 中文比较器,按照拼音排序
08 *
09 * @author KyLinD http://hi.baidu.com/kylind
10 *
11 */
12 @SuppressWarnings("unchecked")
13 public class CnSortComparator implements Comparator {
14
15 RuleBasedCollator collator;
16
17 public CnSortComparator() {
18 collator = (RuleBasedCollator) Collator.getInstance(java.util.Locale.CHINA);
19 }
20
21 public int compare(Object obj1, Object obj2) {
22 String tempname1 = (String) obj1;
23 String tempname2 = (String) obj2;
24 CollationKey c1 = collator.getCollationKey(tempname1);
25 CollationKey c2 = collator.getCollationKey(tempname2);
26 return collator.compare(((CollationKey) c2).getSourceString(),
27 ((CollationKey) c1).getSourceString());
28 }
29 }
02 import java.text.Collator;
03 import java.text.RuleBasedCollator;
04 import java.util.Comparator;
05
06 /**
07 * 中文比较器,按照拼音排序
08 *
09 * @author KyLinD http://hi.baidu.com/kylind
10 *
11 */
12 @SuppressWarnings("unchecked")
13 public class CnSortComparator implements Comparator {
14
15 RuleBasedCollator collator;
16
17 public CnSortComparator() {
18 collator = (RuleBasedCollator) Collator.getInstance(java.util.Locale.CHINA);
19 }
20
21 public int compare(Object obj1, Object obj2) {
22 String tempname1 = (String) obj1;
23 String tempname2 = (String) obj2;
24 CollationKey c1 = collator.getCollationKey(tempname1);
25 CollationKey c2 = collator.getCollationKey(tempname2);
26 return collator.compare(((CollationKey) c2).getSourceString(),
27 ((CollationKey) c1).getSourceString());
28 }
29 }
所有评论,共0条:( 我也来说两句)
代码
