1、实体类 java.lang.Comparable+compareTo
类的写法:
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 新闻条目实体类
* @author Administrator
*
*/
public class NewsItem implements java.lang.Comparable<NewsItem>{//注意泛型
//标题
private String title;
//点击量
private int hits;
//时间
private Date pubTime;
public NewsItem() {
}
public NewsItem(String title, int hits, Date pubTime) {
super();
this.title = title;
this.hits = hits;
this.pubTime = pubTime;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getHits() {
return hits;
}
public void setHits(int hits) {
this.hits = hits;
}
public Date getPubTime() {
return pubTime;
}
public void setPubTime(Date pubTime) {
this.pubTime = pubTime;
}
//实现自定义排序的关键
//时间降序 +点击量升序+标题降序
@Override
public int compareTo(NewsItem o) {
int result =0;
//比较 时间
result =-this.pubTime.compareTo(o.pubTime); //降序
if(0==result){ //时间相同
//点击量
result =this.hits-o.hits; //升序
if(0==result){ //点击量相同
//标题
result=-this.title.compareTo(o.title);//降序
}
}
return result;
}
@Override
public String toString() {
StringBuilder sb =new StringBuilder();
sb.append("标题:").append(this.title);
sb.append(",时间:").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.pubTime));
sb.append(",点击量:").append(this.hits).append("\n");
return sb.toString();
}
}
具体使用:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import com.bjsxt.sort.innerType.Utils;
/**
* 使用Collections
* @author Administrator
*
*/
public class NewsItemApp {
/**
* @param args
*/
public static void main(String[] args) {
List<NewsItem> news=new ArrayList<NewsItem>();
news.add(new NewsItem("美国后怕了,逃跑了悲剧了",50,new Date(System.currentTimeMillis()-1000*60*60)));
news.add(new NewsItem("中国登上钓鱼岛了,全国欢呼了",100,new Date()));
news.add(new NewsItem("小日本终于听话了,泪流满面笑了",60,new Date(System.currentTimeMillis()-1000*60*60)));
System.out.println("排序前:"+news);
//排序
Collections.sort(news);
System.out.println("排序后"+news);
}
}
小结:
1.需要在类里面重写comparTo方法
2.调用Collections.sort()进行排序,参数为容器对象
3.大小规则:0->>相等 <0->>小于 >0->>大于
2、业务排序java.util.Comparator+compare
实体类:
/**
* 实体类
* @author Administrator
*
*/
public class Goods {
//商品名称
private String name;
//价格
private double price;
//收藏量
private int fav;
public Goods() {
}
public Goods(String name, double price, int fav) {
super();
this.name = name;
this.price = price;
this.fav = fav;
}
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 int getFav() {
return fav;
}
public void setFav(int fav) {
this.fav = fav;
}
@Override
public String toString() {
return "商品名:"+name+",收藏量"+this.fav+",价格:"+this.price+"\n";
}
}
实现Comparable接口,提高比较的条件:
package com.bjsxt.sort.refType;
/**
* 按价格排序的业务类 (降序)
* @author Administrator
*
*/
public class GoodsPriceComp implements java.util.Comparator<Goods> {
//重写compare方法
@Override
public int compare(Goods o1, Goods o2) {
return -(o1.getPrice()-o2.getPrice()>0?1:(o1.getPrice()==o2.getPrice()?0:-1));
}
}
测试:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class GoodsApp {
/**
* @param args
*/
public static void main(String[] args) {
List<Goods> list =new ArrayList<Goods>();
list.add(new Goods("老马视频",100,2000));
list.add(new Goods("老高视频",50,2000));
list.add(new Goods("老裴视频",1000,1000));
System.out.println("排序前:"+list);
// Collections.sort(list,new GoodsPriceComp());
Collections.sort(list,new GoodsFavComp());
System.out.println("排序后:"+list);
}
}
小结:
1.优点:
1)解耦,与实体分离
2)方便,应对多种的排序规则。(如有需求还可以写其他的排序类,更改规则即可)
2.需单独写一个类,重写compare方法
3.调用Collections.sort()方法,参数为容器对象和排序类对象。