用Java集合中的Collections.sort方法对list排序

根据Collections.sort重载方法来实现 按照list中的bean的某一个属性进行排序;

//******实体Bean**
package com.iqb.william.test;

public class User {

    private String userName; //姓名

    private Integer age; //年龄 目的:想要根据年龄来进行排序

    private Integer sex; //性别

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getAge() {
        return age;
    }

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

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

}

具体的实现可以看如下代码:

//****具体的排序方法:灵活 可根据某一个属性来进行排序******
package com.iqb.william.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CollectionSortService {

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args) {
    //******定义一个List
        List<User> userList=new ArrayList<User>();

        User user1=new User();
        User user2=new User();
        User user3=new User();

        user1.setUserName("lily");
        user1.setAge(11);
        user2.setUserName("rose");
        user2.setAge(12);
        user3.setUserName("jack");
        user3.setAge(13);

        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        //******************将假设的用户信息放入到list中*********

        //利用重载Collections.sort()方法进行比较list中元素的顺序;
        Collections.sort(userList, new Comparator() {
            @Override
            public int compare(Object arg0, Object arg1) {
                User user1=(User) arg0;
                User user2=(User) arg1;
                return user1.getAge().compareTo(user2.getAge());
            }
        });

        for(User user : userList){
            System.out.println("Name:"+user.getUserName()+"  Age:"+user.getAge());
        }
    }
}

上面的compareTo()方法是调用的Integer.class中的compareTo(xx) 方法,
     前者大于后者那么返回 1;
     前者等于后者那么返回 0;
     前者小于后者那么返回 -1;

    /**
     * Compares two {@code Integer} objects numerically.
     *
     * @param   anotherInteger   the {@code Integer} to be compared.
     * @return  the value {@code 0} if this {@code Integer} is
     *          equal to the argument {@code Integer}; a value less than
     *          {@code 0} if this {@code Integer} is numerically less
     *          than the argument {@code Integer}; and a value greater
     *          than {@code 0} if this {@code Integer} is numerically
     *           greater than the argument {@code Integer} (signed
     *           comparison).
     * @since   1.2
     */
    public int compareTo(Integer anotherInteger) {
        return compare(this.value, anotherInteger.value);
    }

    /**
     * Compares two {@code int} values numerically.
     * The value returned is identical to what would be returned by:
     * <pre>
     *    Integer.valueOf(x).compareTo(Integer.valueOf(y))
     * </pre>
     *
     * @param  x the first {@code int} to compare
     * @param  y the second {@code int} to compare
     * @return the value {@code 0} if {@code x == y};
     *         a value less than {@code 0} if {@code x < y}; and
     *         a value greater than {@code 0} if {@code x > y}
     * @since 1.7
     */
    public static int compare(int x, int y) {
        return (x < y) ? -1 : ((x == y) ? 0 : 1);
    }

以上仅仅是当前遇到的需要根据某个属性进行排序的例子,如果有不对的地方,请指教,谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值