免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1075 | 回复: 0
打印 上一主题 下一主题

Sort ArrayList Object [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-07 15:57 |只看该作者 |倒序浏览
There is a very good way to sort a ArrayList instead of manually checking every element in the ArrayList.

The basic idea is making the element Object implement Comparable interface,and then implement the method "CompareTo" with the logic we want, and Add user Collections class's sort method to sort.


Here is one of the example, we want to sort a list of Competitors first by their last name, if they have the same last name , then sort by first name

Competitor

public class Competitor implements Comparable{
   
    private String lastName;
    private String firstName;
   
    public Competitor(String lastName, String firstName){
        this.lastName = lastName;
        this.firstName = firstName;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public int compareTo(Object o) {
        if(o instanceof Competitor){
            Competitor competitor = (Competitor)o;
            int compareResult = this.getLastName().toUpperCase().compareTo(competitor.getLastName().toUpperCase());
            if(compareResult != 0)
                return compareResult;
            compareResult = this.getFirstName().toUpperCase().compareTo(competitor.getFirstName().toUpperCase());
            if(compareResult !=0)
                return compareResult;
        }else
            return 0;
        return -1;
    }
}
And here is the testing code

public class TestCompare {
   
   
    @SuppressWarnings({ "unchecked", "unchecked", "unchecked" })
    public static void main(String[] args){
        Competitor comp1 = new Competitor("Abc","bcd");
        Competitor comp2 = new Competitor("Bcd","abc");
        Competitor comp3 = new Competitor("bcd","bcd");
        
        ArrayListCompetitor> dataList = new ArrayListCompetitor>();   
        dataList.add(comp2);
        dataList.add(comp1);
        dataList.add(comp3);
        for(int i=0; idataList.size();i++){
            Competitor comp = dataList.get(i);
            System.out.println("Before:"+comp.getLastName()+"\t"+comp.getFirstName());
        }
    // if (comp1 instanceof Comparable){
        Collections.sort(dataList);
        System.out.println("\n");
    /// }
     
     for( int i=0; idataList.size(); i++){
         Competitor comp = dataList.get(i);
         System.out.println("After:"+comp.getLastName()+"\t"+comp.getFirstName());
     }
    }
        
}
and here is the result

Before:Bcd abc
Before:Abc bcd
Before:bcd bcd
After:Abc bcd
After:Bcd abc
After:bcd bcd


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/2956/showart_272709.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP