使用 Collectio.sort() 对单个和多个条件进行排序
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Comparator;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.DateFormat;
public class SortingWithCollection {
public static void main(String[] args) {
List<Student> listStudent = new ArrayList<Student>();
listStudent.add(new Student("Rahul", "01", 45, "2020-11-15T11:01:10.000Z"));
listStudent.add(new Student("Dharma", "08", 30, "2020-11-15T11:22:50.000Z"));
listStudent.add(new Student("Lalit", "10", 45, "2020-11-15T11:05:10.000Z"));
listStudent.add(new Student("Harshit", "02", 25,"2020-11-15T11:22:10.000Z"));
listStudent.add(new Student("Mohit", "05", 45, "2020-11-15T11:12:30.000Z"));
listStudent.add(new Student("Ram", "09", 30,"2020-11-15T11:22:10.000Z"));
listStudent.add(new Student("Lalu", "10", 25, "2020-11-15T11:28:10.000Z"));
listStudent.add(new Student("Sohan", "12", 30, "2020-11-15T11:22:10.000Z"));
listStudent.add(new Student("Neha", "04", 22, "2020-11-15T11:22:10.000Z"));
listStudent.add(new Student("Aaradhya", "01", 28,"2020-11-15T11:22:14.000Z"));
listStudent.add(new Student("Aakriti", "06", 35, "2020-11-15T11:22:10.000Z"));
listStudent.add(new Student("Sonali", "05", 35, "2020-11-15T11:35:01.000Z"));
listStudent.add(new Student("Billo", "08", 35, "2020-11-15T11:22:10.000Z"));
listStudent.add(new Student("Sohan", "05", 35, "2020-11-15T11:22:10.000Z"));
listStudent.add(new Student("Rohan", "02", 30, "2020-11-15T11:58:12.000Z"));
System.out.println("Before sorting List Is:");
for (Student student : listStudent) {
System.out.println(student);
}
Collections.sort(listStudent,new SortByAgeAndDate());
// Collections.sort(listStudent,new SortByClass());
System.out.println("After sorting List is:");
for (Student student : listStudent) {
System.out.println(student);
}
}
public static class SortByClass implements Comparator<Student> {
@Override
public int compare(Student student1, Student student2) {
return student2.getClassName().compareTo(student1.getClassName());
}
}
public static class SortByAgeAndDate implements Comparator<Student>{
@Override
public int compare(Student student1, Student student2) {
if(student1.getAge()==student2.getAge()){
return
student2.getDateCreatedAt().compareTo(student1.getDateCreatedAt());
}else{
return Integer.compare(student2.getAge(),student1.getAge());
}
}
}
public static class Student {
String name;
String className;
int age;
String createdAt;
Date dateCreatedAt;
public Student(String name, String className, int age, String createdAt) {
this.name = name;
this.className = className;
this.age = age;
this.createdAt=createdAt;
}
public int getAge(){
return age;
}
public String getName(){
return name;
}
public String getClassName(){
return className;
}
public String getCreatedAt(){
return createdAt;
}
public Date getDateCreatedAt(){
try{
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
dateCreatedAt = dateFormat.parse(createdAt);
}catch(Exception e){
System.out.println(""+e.getMessage());
return null;
}
return dateCreatedAt;
}
public String toString() {
return String.format("%s\t%s\t%d\t%s", name, className, age,createdAt);
}
}
}