ArrayList集合数据排序问题

核心方法就是使用Collections的sort方法

Collections.sort(comparator ,  mList);

让 Student 实现Comparable接口,或是实例化一个比较器,现在用 Comparator 比较器实例来做一个。

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


public class ComparableTest {
 public static void main(String[] args) {
    Comparator comparator = new Comparator(){
     public int compare(Student s1, Student s2) {
      //先排年龄
      if(s1.age!=s2.age){
       return s2.age-s1.age;
      }
      else{
       //年龄相同则按姓名排序
       if(!s1.name.equals(s2.name)){
        return s2.name.compareTo(s1.name);
       }
       else{
        //姓名也相同则按学号排序
        return s2.id-s1.id;
       }
      }
     }
    };
    Student stu1 = new Student (1,"zhangsan","male",28,"cs");
    Student stu2 = new Student (2,"lisi","female",19,"cs");
    Student stu3 = new Student (3,"wangwu","male",22,"cs");
    Student stu4 = new Student (4,"zhaoliu","female",17,"cs");
    Student stu5 = new Student (5,"jiaoming","male",22,"cs");

    ArrayList List = new ArrayList();
    List.add(stu1);
    List.add(stu2);
    List.add(stu3);
    List.add(stu4);
    List.add(stu5); 
    //这里就会自动根据规则进行排序
    Collections.sort(List,comparator);
    display(List);
   }

   static void display(ArrayList lst){
    for(Student s:lst)
     System.out.println(s);
   }
  }

  class Student{
   int age;
   int id;
   String gender;
   String name;
   String cs;
   Student(int id,String name,String gender,int age,String cs){
    this.age=age;
    this.name=name;
    this.gender=gender;
    this.id=id;
    this.cs=cs;
   }
   public String toString(){
    return id+"  "+name+"  "+gender+"  "+age+"  "+cs;
   }
  }

你可能感兴趣的:(java基础)