/*
*学习功能: 排序算法之 冒泡法:即 重量轻的上浮
*整理:孔华生 2012-10-28
*/
public class TestBubblesort{
public static void main(String args[])
{
Date[] days = new Date[5];
days[0] = new Date(2006,5,4);
days[1] = new Date(2006,7,4);
days[2] = new Date(2008,5,4);
days[3] = new Date(2004,5,9);
days[4] = new Date(2004,5,4);
Bubblesort(days);
for(int i=0;i<days.length;i++)
{
System.out.println(days[i]);
}
}
public static void Bubblesort(Date[] b){
int len = b.length;
for(int i = len-1;i>=1;i--){
for(int j=0;j<=i-1;j++)
{
if(b[j].compare(b[j+1])>0)
{
Date temp = b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
}
}
class Date{
int year,month,day;
//定义构造方法
Date(int _year,int _month,int _day)
{
year = _year;
month = _month;
day = _day;
}
//定义普通方法 具有返回值的普通方法
public int compare(Date date)
{
//三目运算
return year > date.year ? 1
:year < date.year? -1
:month > date.month ? 1
:month < date.month ? -1
:day >date.day ? 1
: day < date.day ? -1:0 ;
}
public String toString()
{
return "year:month:day---"+year+"-"+month+"-"+day;
}
}