Comparable使用
public static void main(String[] args) {
demo d = new demo();
Custumer c1 = d.new Custumer(1, 1, "1");
Custumer c2 = d.new Custumer(4, 2, "2");
Custumer c3 = d.new Custumer(2, 3, "3");
Custumer c4 = d.new Custumer(2, 4, "4");
Custumer c5 = d.new Custumer(5, 5, "5");
List<Custumer> custumers = new ArrayList<demo.Custumer>();
custumers.add(c1);
custumers.add(c2);
custumers.add(c3);
custumers.add(c4);
custumers.add(c5);
for(Custumer custumer:custumers){
System.out.print(" "+custumer.getName());
}
System.out.println("\n----------------------");
Collections.sort(custumers);
for(Custumer custumer:custumers){
System.out.print(" "+custumer.getName());
}
}
class Custumer implements Comparable<Custumer> {
private int id;
private int age;
private String name;
public Custumer(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int compareTo(Custumer o) {
if (o.getId() > id) {
return -1;
} else if (o.getId() < id) {
return 1;
} else {
if (o.getAge() > age) {
return -1;
} else if (o.getAge() < age) {
return 1;
} else {
return 0;
}
}
}
}
Compartor使用
package test;
import java.util.Comparator;
public class ComparatorConsunInfo implements Comparator<ConsumInfo> {
@Override
public int compare(ConsumInfo o1, ConsumInfo o2) {
if(o1.getPrice() > o2.getPrice()){
return 1;
}
if(o1.getPrice() < o2.getPrice()){
return -1;
}
if(o1.getPrice() == o2.getPrice()){
if(o1.getUid() > o2.getUid()){
return 1;
}
if(o1.getUid() < o2.getUid()){
return -1;
}
}
return 0;
}
}
public class ConsumInfo{
private int uid;
private String name;
private double price;
private Date datetime;
public ConsumInfo() {
}
public ConsumInfo(int uid,String name,double price,Date datetime){
this.uid = uid;
this.name = name;
this.price = price;
this.datetime = datetime;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Date getDatetime() {
return datetime;
}
public void setDatetime(Date datetime) {
this.datetime = datetime;
}
@Override
public String toString() {
return "ConsumInfo [uid=" + uid + ", name=" + name + ", price=" + price
+ ", datetime=" + datetime + "]";
}
}
public class ConsumInfoTest {
public static void main(String[] args) {
ConsumInfo consumInfo1 = new ConsumInfo(100, "consumInfo1", 400.0,new Date());
ConsumInfo consumInfo2 = new ConsumInfo(200, "consumInfo1", 200.0,new Date());
ConsumInfo consumInfo3 = new ConsumInfo(300, "consumInfo1", 100.0,new Date());
ConsumInfo consumInfo4 = new ConsumInfo(400, "consumInfo1", 700.0,new Date());
ConsumInfo consumInfo5 = new ConsumInfo(500, "consumInfo1", 800.0,new Date());
ConsumInfo consumInfo6 = new ConsumInfo(600, "consumInfo1", 300.0,new Date());
ConsumInfo consumInfo7 = new ConsumInfo(700, "consumInfo1", 900.0,new Date());
ConsumInfo consumInfo8 = new ConsumInfo(800, "consumInfo1", 400.0,new Date());
List<ConsumInfo> list = new ArrayList<ConsumInfo>();
list.add(consumInfo1);
list.add(consumInfo2);
list.add(consumInfo3);
list.add(consumInfo4);
list.add(consumInfo5);
list.add(consumInfo6);
list.add(consumInfo7);
list.add(consumInfo8);
System.out.println("排序前:");
for(ConsumInfo consumInfo : list ){
System.out.println(consumInfo);
}
ComparatorConsunInfo comparatorConsunInfo = new ComparatorConsunInfo();
Collections.sort(list,comparatorConsunInfo);
System.out.println("排序后:");
for(ConsumInfo consumInfo :list){
System.out.println(consumInfo);
}
}
}