数据结构之排序算法 对象数据排序2

import java.util.Arrays;

/*
	 演示用Arrays.sort()来排序
	 */
public class sortObjectDemo2 {
	public static void main(String[] args) {
		DataOBject2 objs[] = new DataOBject2[6];
		objs[0] = new DataOBject2("Jack", new MyDatee(1990,1,1), 1.5);
		objs[1] = new DataOBject2("Tom", new MyDatee(1991,3,1), 20);
		objs[2] = new DataOBject2("Rose", new MyDatee(1993,5,1), -88.8);
		objs[3] = new DataOBject2("Mike", new MyDatee(1995,10,1), 10);
		objs[4] = new DataOBject2("Kobe", new MyDatee(1980,1,6), -5);
		objs[5] = new DataOBject2("James", new MyDatee(2018,3,18), 0);
		
		Arrays.sort(objs);
		
		for(Object obj:objs){
			System.out.println(obj);
		}
  }
}

public class DataOBject2 implements Comparable<DataOBject2>{
    private String name;
    private MyDatee birthday;
    private double key;代表用于决定排序顺序的字段,你可以理解成年龄、成绩、金额等等
	public DataOBject2(String name, MyDatee birthday, double key) {
		this.name = name;
		this.birthday = birthday;
		this.key = key;
	}
	public DataOBject2() {
		super();
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public MyDatee getBirthday() {
		return birthday;
	}
	public void setBirthday(MyDatee birthday) {
		this.birthday = birthday;
	}
	public double getKey() {
		return key;
	}
	public void setKey(double key) {
		this.key = key;
	}
	@Override
	public String toString() {
		return  name + "," + birthday+ ", " + key ;
	}
	@Override
	public int compareTo(DataOBject2 o) {
		/*if(this.key>o.key){
			return -1;
		}else if(this.key<o.key){
			return 1;
		}else{
			return 0;
		}*/
		return -(this.getBirthday().getYear()-o.birthday.getYear());
	}
    
}
public class MyDatee {
     private int year;
     private int month;
     private int day;
	public MyDatee(int year, int month, int day) {
		super();
		this.year = year;
		this.month = month;
		this.day = day;
	}
	
	public MyDatee() {
		super();
	}

	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public int getDay() {
		return day;
	}
	public void setDay(int day) {
		this.day = day;
	}
	@Override
	public String toString() {
		return "["+ year + "年" + month + "月" + day
				+ "日]";
	} 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值