Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2087879
  • 博文数量: 1647
  • 博客积分: 80000
  • 博客等级: 元帅
  • 技术积分: 9980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 15:15
文章分类

全部博文(1647)

文章存档

2011年(1)

2008年(1646)

我的朋友

分类:

2008-10-28 18:33:05

第一种情况:

  1.                   Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
  2.         
  3.         String[] arr = { "张三", "李四", "王五", "刘六" };
  4.         Arrays.sort(arr, cmp);
  5.         for (int i = 0; i < arr.length; i++)
  6.             System.out.println(arr[i]);


第二种情况:

//ComparableBean.java
import java.text.CollationKey;
import java.text.Collator;
import java.text.RuleBasedCollator;
import java.util.Comparator;

public class ComparableBean{
    private String name;
    
    public ComparableBean(String name) {
        
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
class ComparableBeanComparator  implements Comparator//
{
    RuleBasedCollator collator; // you can set your rules for the instance "collator"
    public ComparableBeanComparator()
    {
        collator = (RuleBasedCollator)Collator.getInstance(java.util.Locale.CHINA);// try testing various locales
    }
    public int compare(Object obj1, Object obj2) {
        String tempname1 = ((ComparableBean) obj1).getName();
        String tempname2 = ((ComparableBean) obj2).getName();
        
        CollationKey c1 = collator.getCollationKey(tempname1);
        CollationKey c2 = collator.getCollationKey(tempname2);
//        return collator.compare(((CollationKey) c1).getSourceString(),
//                ((CollationKey) c2).getSourceString());
        return collator.compare(((CollationKey) c2).getSourceString(),
                ((CollationKey) c1).getSourceString());
    }
//    public int compare(ComparableBean obj1, ComparableBean obj2) {
//        String tempname1 = obj1.getName();
//        String tempname2 = obj2.getName();
//        
//        CollationKey c1 = collator.getCollationKey(tempname1);
//        CollationKey c2 = collator.getCollationKey(tempname2);
//        return collator.compare(((CollationKey) c1).getSourceString(),
//                ((CollationKey) c2).getSourceString());
//    }
}
//the end of ComparableBean.java


测试代码:


                  ComparableBean[] nameContent = { new ComparableBean("一切从实际出发"),
                new ComparableBean("立于不败之地"), new ComparableBean("多项式"),
                new ComparableBean("贯彻落实"), new ComparableBean("密切联系群众"),
                new ComparableBean("四项基本原则"), new ComparableBean("咬牙切齿"),
                new ComparableBean("恭恭敬敬"), new ComparableBean("民警"),
                new ComparableBean("经营承包责任制") };
        Arrays.sort(nameContent,new ComparableBeanComparator());
        for (int i = 0; i < nameContent.length; i++) {
            System.out.println(nameContent[i].getName());
        }
【责编:Peng】

--------------------next---------------------

阅读(623) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~