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());
      }
     }
    }

  • 相关阅读:
    毫秒倒计时小Demo
    css3 翻转
    canvas
    html5
    css3
    一些免费的svn空间(SVN代码托管)
    Xcode 6制作动态及静态Framework
    ios 动态执行的代码
    ios nsarray对象问题
    iOS xcode 编译选项 architecture(cup架构问题)
  • 原文地址:https://www.cnblogs.com/sm21312/p/3907355.html
Copyright © 2011-2022 走看看