Java编程中如何实现中文排序
- JAVA认证
- 关注:3K次
在不断的Java学习中,我们就能发现在很多的时候还是有不少的技巧。下面YJBYS小编为大家整理了关于Java编程中如何实现中文排序的文章,希望对你有所帮助。
第一种情况:
Comparator cmp = nstance(A);
String[] arr = { “张三”, “李四”, “王五”, “刘六” };
(arr, cmp);
for (int i = 0; i 《 th; i++)
tln(arr[i]);
第二种情况:
//
import ationKey;
import ator;
import BasedCollator;
import arator;
public class ComparableBean{
private String name;
public ComparableBean(String name) {
= name;
}
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
}
class ComparableBeanComparator implements Comparator//
{
RuleBasedCollator collator; // you can set your rules for the instance “collator”
public ComparableBeanComparator()
{
collator = (RuleBasedCollator)nstance(A);// try testing various locales
}
public int compare(Object obj1, Object obj2) {
String tempname1 = ((ComparableBean) obj1)。getName();
String tempname2 = ((ComparableBean) obj2)。getName();
CollationKey c1 = ollationKey(tempname1);
CollationKey c2 = ollationKey(tempname2);
// return are(((CollationKey) c1)。getSourceString(),
// ((CollationKey) c2)。getSourceString());
return are(((CollationKey) c2)。getSourceString(),
((CollationKey) c1)。getSourceString());
}
// public int compare(ComparableBean obj1, ComparableBean obj2) {
// String tempname1 = ame();
// String tempname2 = ame();
//
// CollationKey c1 = ollationKey(tempname1);
// CollationKey c2 = ollationKey(tempname2);
// return are(((CollationKey) c1)。getSourceString(),
// ((CollationKey) c2)。getSourceString());
// }
}
//the end of
测试代码:
ComparableBean[] nameContent = { new ComparableBean(“一切从实际出发”),
new ComparableBean(“立于不败之地”), new ComparableBean(“多项式”),
new ComparableBean(“贯彻落实”), new ComparableBean(“密切联系群众”),
new ComparableBean(“四项基本原则”), new ComparableBean(“咬牙切齿”),
new ComparableBean(“恭恭敬敬”), new ComparableBean(“民警”),
new ComparableBean(“经营承包责任制”) };
(nameContent,new ComparableBeanComparator());
for (int i = 0; i 《 th; i++) {
tln(nameContent[i]。getName());
}
- 文章版权属于文章作者所有,转载请注明 https://xuezhezhai.com/itrz/java/xdq6ew.html