首页 > 编程语言 > 详细

list对象排序问题

时间:2017-08-09 09:39:48      阅读:157      评论:0      收藏:0      [点我收藏+]
public class ListSort {
    public static void main(String[] args) {
      List<User> userList = new ArrayList<>();
      userList.add(new User("dd", 4));
      userList.add(new User("aa", 6));
      userList.add(new User("ee", 5));
      userList.add(new User("gg", 6));
      List<User> userList2 = new ArrayList<>();
      userList.add(new User("vv", 10));
      userList.addAll(userList2);

      Collections.sort(userList, new Comparator<User>() {
      @Override
      public int compare(User o1, User o2) {
          if (o1.getAge() > o2.getAge()) {
              return -1;
          }
          if (o1.getAge() == o2.getAge()) {
              return 0;
          }
          return 1;
        }
      });

      for (User user : userList) {
          System.out.println(user.getAge() + "," + user.getName());
      }
  }

  class User {
      String name;
      Integer age;
      public User(String name, Integer age) {
          this.name = name;
          this.age = age;
      }
      public Integer getAge() {
          return age;
      }
      public String getName() {
          return name;
      }
  }
}

 

list对象排序问题

原文:http://www.cnblogs.com/dali-lyc/p/7323255.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!