一、比较器
在Java中对基本类型进行排序可以通过Array.sort()和Collections.sort(),默认是升序
在Java中对对象进行排序可以通过Compare和CompareTo,并且也可以对基本数据类型进行排序
注意:如果想要对自定义类进行排序,需要重写CompareTo或Compare方法
1、Compare
Compare是Comparator接口中的一个方法,用于比较对象和对象的顺序【对象排序】
compare方法:
int compare(T o1,T o2)
CompareTo方法返回一个int类型的数据
如果该对象小于指定对象,返回负整数
如果该对象等于指定对象,返回0
如果该对象大于指定对象,返回正整数
代码案例:
package cn.com.example11;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Student implements Comparator<Student> {
private int id;
private String name;
private int age;
private String sex;
public Student() {
}
public Student(int id, String name, int age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
@Override
public int compare(Student o1, Student o2) {
if(o1.getAge() - o2.getAge() == 0){
return o1.getName().compareTo(o2.getName());
}
return o1.getAge() - o2.getAge();
}
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student(1,"张三",20,"男"));
list.add(new Student(2,"李四",21,"女"));
list.add(new Student(3,"王五",19,"女"));
list.add(new Student(4,"赵六",19,"男"));
list.add(new Student(5,"田七",18,"男"));
// 排序
Collections.sort(list,new Student());
for (Student student : list) {
System.out.println(student);
}
}
}
2、CompareTo
CompareTo是Comparable接口中的一个方法,用于比较对象和对象的顺序【对象排序】
compareTo方法:
int compareTo(T o)
CompareTo方法返回一个int类型的数据
如果该对象小于指定对象,返回负整数
如果该对象等于指定对象,返回0
如果该对象大于指定对象,返回正整数
代码案例:
package cn.com.example11;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Student implements Comparable<Student>{
private int id;
private String name;
private int age;
private String sex;
public Student() {
}
public Student(int id, String name, int age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
@Override
public int compareTo(Student o) {
// 年龄从小到大排序,如果相等通过姓名排序
if(this.getAge() - o.getAge() == 0){
return this.getName().compareTo(o.getName());
}
return this.getAge() - o.getAge();
}
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student(1,"张三",20,"男"));
list.add(new Student(2,"李四",21,"女"));
list.add(new Student(3,"王五",19,"女"));
list.add(new Student(4,"赵六",19,"男"));
list.add(new Student(5,"田七",18,"男"));
// 排序
Collections.sort(list);
for (Student student : list) {
System.out.println(student);
}
}
}