1.让对象所在的类,实现Comparable接口
2.方法重写compareTo();
3.完成比较规则的制定
对象比较
根据age去比
public class Person implements Comparable {
String name;
int age;
double salary;
public Person() {
}
public Person(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
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 double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
@Override
public int compareTo(Object o) {
Person p2 = (Person)o;
return this.age-p2.age;
}
}
public class Test {
public static void main(String[] args) {
Person p1 = new Person("李白",20,999.9);
Person p2 = new Person("杜甫",30,999.8);
int i = p1.compareTo(p2);
System.out.println(i);
}
}
根据薪水去比
package com.atguigu.Compare.c1;
public class Person implements Comparable {
String name;
int age;
double salary;
public Person() {
}
public Person(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
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 double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
@Override
public int compareTo(Object o) {
Person p = (Person)o;
if (this.salary>p.salary){
return 1;
}else if(this.salary<p.salary){
return -1;
}else{
return 0;
}
//return Double.compare(this.salary, p.salary);
}
}
public class Test {
public static void main(String[] args) {
Person p1 = new Person("李白",20,999.9);
Person p2 = new Person("杜甫",30,999.8);
int i = p1.compareTo(p2);
System.out.println(i);
}
}
数组比较
按照薪资比较
import com.atguigu.innerclass.in4.exer.Emplyee;
public class Employee implements Comparable{
int id;
String name;
double salary;
public Employee() {
}
public Employee(int id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", salary=" + salary +
'}';
}
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 double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public int compareTo(Object o) {
Employee e = (Employee)o;
/*按照薪资比较*/
return Double.compare(this.salary, e.salary);
}
}
import com.atguigu.innerclass.in4.exer.Emplyee;
import java.util.concurrent.ForkJoinPool;
public class Test {
public static void main(String[] args) {
Employee e1 = new Employee(1,"张三",9999.9);
Employee e2 = new Employee(6,"李四",9999.1);
Employee e3 = new Employee(3,"王五",9999.3);
Employee e4 = new Employee(4,"赵六",9999.6);
Employee e5 = new Employee(2,"陈七",9999.8);
Employee [] es = {e1,e2,e3,e4,e5};
/*利用冒泡排序*/
for (int i =0;i<es.length-1;i++){
for(int j=0;j<es.length-1-i;j++){
if (es[j].compareTo(es[j + 1])>0) {
Employee temp = es[j];
es[j]=es[j+1];
es[j+1] = temp;
}
}
}
for (Employee ele : es){
System.out.println(ele);
}
}
}
薪资相同时候,按照id比
只需要修改Comparable的重写方法:compareTo
public int compareTo(Object o) {
Employee e = (Employee)o;
/*薪资相同,按照id比较*/
if(this.salary==e.salary){
return this.id - e.id;
}
return Double.compare(this.salary, e.salary);
}
数组通用工具类比较
冒泡排序换为,也需要重写compareTo方法,制定比较规则。
Arrays.sort(es);