码迷,mamicode.com
首页 > 编程语言 > 详细

list对象排序问题

时间:2017-08-09 10:12:45      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:sys   tor   system   dal   nbsp   ==   pre   log   blog   

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对象排序问题

标签:sys   tor   system   dal   nbsp   ==   pre   log   blog   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!