java集合中对象某属性比较排序Comparable与Comparator

要对集合中的对象的某属性进行排序有两种方式。

a. 一种是要排序对象类实现comparable接口的compareTo方法;然后把对象放入list;然后调用Collections.sort(list);
b. 一种是不对要排序对象类做任何改动,创建Comparator接口的实现类C;然后 把对象放入list;然后调用Collections.sort(list, C);

在对象中定义排序


对象中数值型排序
public class User implements Comparable<User>{
    private String name;
    private int age;
    private int gender;
    private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public User() {
        super();
    }

    public User(String name, int age, int gender, String address) {
        super();
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.address = address;
    }

    @Override
    public int compareTo(User o) {
        if(o!=null){
            if(this.getAge()>o.getAge()){

               return 1;
            }else if(this.getAge()==o.getAge()){

               return 0;
            }

       }
        return -1;
    }
}
############################# main ##################
List<User> ulist = new ArrayList<User>();
ulist.add(new User("wangbo",29,1,"长沙"));
ulist.add(new User("wumei",44,1,"株洲"));
ulist.add(new User("zhangjie",19,1,"岳阳"));
ulist.add(new User("lihua",36,1,"长沙"));
ulist.add(new User("zhangchangzhe",19,1,"衡阳"));

Collections.sort(list);

for(User u:ulist){
            System.out.println(u.getName()+"\t"+u.getAge()+"\t"+u.getAddress());
}

不该动对象类

public class User {
    private String name;
    private int age;
    private int gender;
    private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public User() {
        super();
    }

    public User(String name, int age, int gender, String address) {
        super();
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.address = address;
    }

}
############################################################
import java.util.Comparator;

public class ComparatorImpl implements Comparator<User>{

    @Override
    public int compare(User o1, User o2) {

        if(null!=o1 && null!=o2){
            if(o1.getAge() >o2.getAge()){
                return 1;
            }else if(o1.getAge() ==o2.getAge()){
                return 0;
            }
        }
        return -1;
    }

}

########################### main 方法 ################################
List<User> ulist = new ArrayList<User>();
        ulist.add(new User("wangbo",29,1,"长沙"));
        ulist.add(new User("wumei",44,1,"株洲"));
        ulist.add(new User("zhangjie",19,1,"岳阳"));
        ulist.add(new User("lihua",36,1,"长沙"));
        ulist.add(new User("zhangchangzhe",19,1,"衡阳"));
        Collections.sort(ulist, new ComparatorImpl());
        for(User u:ulist){
            System.out.println(u.getName()+"\t"+u.getAge()+"\t"+u.getAddress());
        }
对象中字符串排序
public class HahaTime2 {  

    public static void main(String[] args) {  
        /** 
         * 原来的日期 
         */  
        List<Person> list = new ArrayList<Person>();  
        list.add(new Person(1,"2016-04-22 15:30:28", "小芳"));  
        list.add(new Person(1,"2016-03-02 15:30:28", "小花"));  
        list.add(new Person(1,"2016-02-12 15:30:28", "小狗"));  
        list.add(new Person(1,"2016-12-02 15:30:28", "小猫"));  
        list.add(new Person(1,"2016-02-02 15:30:28", "小米"));  
        list.add(new Person(1,"2016-02-12 15:30:28", "小明"));  

        Collections.sort(list, new MapComparator());  
        for (Person person : list) {  
            System.out.println(person.name+";"+person.beginTime);  
        }  
    }  

}  
class MapComparator implements Comparator<Person>{  

    public int compare(Person lhs, Person rhs) {  
        return lhs.beginTime.compareTo(rhs.beginTime);  
    }  

}  

装载 http://blog.csdn.net/jbgtwang/article/details/8765645

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值