zoukankan      html  css  js  c++  java
  • java按照集合中元素的属性进行排序示例代码

    public class Student {
     private String name;
     private int age;
     private int id;
     public Student() {
      super();
     }
     public Student(String name, int age, int id) {
      super();
      this.name = name;
      this.age = age;
      this.id = id;
     }
     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 getId() {
      return id;
     }
     public void setId(int id) {
      this.id = id;
     }
     @Override
     public String toString() {
      return "id:"+id+" age"+age+" name"+name;
     }
    }

    public class Test {
     public static void main(String[] args) {
      ArrayList<Student> list = new ArrayList<Student>();
      list.add(new Student("张三",23,1));
      list.add(new Student("张三",24,4));
      list.add(new Student("李四",22,2));
      list.add(new Student("王五",21,3));
      Collections.sort(list,new Comparator<Student>() {
       public int compare(Student s1, Student s2) {
        if(!s1.getName().equals(s2.getName())){
         return s2.getName().compareToIgnoreCase(s1.getName());
        }
        if(s1.getAge()>s2.getAge()){
         return 1;// return 1代表需要交换位置
        }
        return -1;
       }
      });
      
      for (int i = 0; i < list.size(); i++) {
       System.out.println(list.get(i).toString());
      }
     }
    }

  • 相关阅读:
    一 数据库备份与恢复 2 数据库恢复 2.2 数据库重定向与重建
    附录 常用SQL语句 Dynamic SQL
    alt_disk_install 克隆系统rootvg
    Mysql版本升级
    DB29.7 HADR环境升级
    EMC VNX系列存储维护
    保存最开始的flink code,  数据是自动生成而不是通过kafka
    opentsdb restful api使用方法
    flink 和 hbase的链接
    opentsdb
  • 原文地址:https://www.cnblogs.com/sm21312/p/3907355.html
Copyright © 2011-2022 走看看