当我们要对集合中的对象进行排序是,需要使用Comparable&Comparator,从而实现对象的排序。
一、使用Comparable接口完成排序:实现此接口的对象列表可以通过Collection.sort进行自动排序。
package com.wjl.study.sort.object;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import com.alibaba.fastjson.JSON;
public class Student implements Comparable<Student> {
private String name;
private int age;
private String number;
public Student() {
}
public Student(String name, int age, String number) {
this.name = name;
this.age = age;
this.number = number;
}
@Override
public int compareTo(Student stu) {
return number.compareTo(stu.number);
}
public String toString() {
//使用json进行数据拼装,需要进行fastjson jar包
return JSON.toJSONString(this);
}
// 通过学号进行排序
public static void main(String[] args) {
//
final int flag = -1;
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student("张三", 30, "200906084130"));
list.add(new Student("李四", 24, "200906084111"));
list.add(new Student("王五", 66, "200906084166"));
list.add(new Student("赵六", 34, "200906084132"));
System.out.println("before sort: " + list);
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
//降序
return o1.compareTo(o2) * flag;
}
});
System.out.println("after sort1: " + list);
//升序
Collections.sort(list);
System.out.println("after sort2: " + list);
}
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 getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
二、使用Comparator接口实现排序:实现compare方法,通过比较参数大小的返回值来进行对象排序。
package com.wjl.study.sort.objectsort;
public class Student {
private String name;
private int age;
private String number;
public Student(String name, int age, String number) {
this.name = name;
this.age = age;
this.number = number;
}
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 getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
package com.wjl.study.sort.objectsort; import java.util.Comparator; /** * 使用Comparator接口实现排序 * * @author weijunlong * */ public class MyCompare implements Comparator<Student> { private int flag = 1; public MyCompare() { } public MyCompare(int flag) { this.flag = flag; } @Override public int compare(Student stu1, Student stu2) { return stu1.getNumber().compareTo(stu2.getNumber()) * flag; } }
package com.wjl.study.sort.objectsort; import java.util.ArrayList; import java.util.Collections; public class Test { public static void main(String args[]) { ArrayList<Student> list = new ArrayList<Student>(); list.add(new Student("张三", 30, "200906084130")); list.add(new Student("李四", 24, "200906084111")); list.add(new Student("王五", 66, "200906084166")); list.add(new Student("赵六", 34, "200906084132")); System.out.println("before sort: " + list); Collections.sort(list, new MyCompare(1)); System.out.println("after sort: " + list); } }