两种方法:一种是在类内部实现;一种是在类外部定义一个类的可比器
package in;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
/**
* Created by S_eve on 2017/6/16.
* 关于排序的两个接口
*/
public class TestJiakou {
public static void main(String[] args) {
//第一种方法
Student[] students1 = new Student[7];
students1[0] = new Student(7, "hah");
students1[1] = new Student(3, "hah");
students1[2] = new Student(5, "hah");
students1[3] = new Student(1, "hah");
students1[4] = new Student(4, "hah");
students1[5] = new Student(2, "hah");
students1[6] = new Student(6, "hah");
Arrays.sort(students1);
for(int i = 0; i < 7; i++){
System.out.println(students1[i].toString());
}
System.out.println("+++++++++++++++++++++++++++++++++++++");
//第二种
Student1[] students2 = new Student1[7];
students2[0] = new Student1(7, "hah");
students2[1] = new Student1(3, "hah");
students2[2] = new Student1(5, "hah");
students2[3] = new Student1(1, "hah");
students2[4] = new Student1(4, "hah");
students2[5] = new Student1(2, "hah");
students2[6] = new Student1(6, "hah");
Arrays.sort(students2, new StudentComparator());
for(int i = 0; i < 7; i++){
System.out.println(students2[i].toString());
}
}
}
//第一种方法,在类内部实现Comparable
class Student implements Comparable{
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
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;
}
@Override
public boolean equals(Object o) {
if(this == o)
return true;
if(!(o instanceof Student))
return false;
Student other = (Student)o;
if(this.id == other.id && this.name.equals(other.name))
return true;
return false;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
@Override
public int compareTo(Object o) {
Student other = (Student)o;
if(this.id < other.id){
return -1;
}else if(this.id > other.id) {
return 1;
}
return 0;
}
}
class Student1{
private int id;
private String name;
public Student1(int id, String name) {
this.id = id;
this.name = name;
}
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;
}
@Override
public boolean equals(Object o) {
if(this == o)
return true;
if(!(o instanceof Student1))
return false;
Student1 other = (Student1)o;
if(this.id == other.id && this.name.equals(other.name))
return true;
return false;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Student1{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
//第二种定义一个类比较器
class StudentComparator implements Comparator{
@Override
public int compare(Object o1, Object o2) {
Student1 other1 = (Student1)o1;
Student1 other2 = (Student1)o2;
if(other1.getId() < other2.getId()){
return -1;
}else if(other1.getId() > other2.getId()){
return 1;
}
return 0;
}
}