5.Collections的sort方法进行排序

一、Comparable的compareto方法,进行排序

package mypro01;

import java.text.SimpleDateFormat;
import java.util.Date;

//新闻条目实体类
//实体类实现java.lang.Comparable+compareTo方法
public class NewItem implements Comparable<NewItem>{
	//标题
	private String title;
	//点击量
	private  int  hits;
	//时间
	private  Date  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;
	}
	public NewItem(String title, int hits, Date pubtime) {
		super();
		this.title = title;
		this.hits = hits;
		this.pubtime = pubtime;
	}
	public NewItem() {
		
	}
	@Override
	public int compareTo(NewItem 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(this.hits);
		sb.append("时间").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(pubtime)).append("\n");
		return sb.toString();
	}	
	
} 
package mypro01;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class NewItemApp {
    public static void main(String[] args) {
        List<NewItem> news=new ArrayList<NewItem>();
        news.add(new NewItem("中国",1000000,new Date()));
        news.add(new NewItem("日本",1233433,new Date(System.currentTimeMillis()-1000
                *60*60)));
        news.add(new NewItem("上海",123323,new Date(System.currentTimeMillis()-1000
                *60*60)));
        
        System.out.println("排序前"+news);
        Collections.sort(news);
        System.out.println("排序后"+news);
    }
}
结果:排序前[标题中国,点击量1000000时间2022-03-27 16:06:19
, 标题日本,点击量1233433时间2022-03-27 15:06:19
, 标题上海,点击量123323时间2022-03-27 15:06:19
]
排序后[标题中国,点击量1000000时间2022-03-27 16:06:19
, 标题上海,点击量123323时间2022-03-27 15:06:19
, 标题日本,点击量1233433时间2022-03-27 15:06:19
]

二、Comparator接口,重写compare方法,独立于实体类,便于应对各种排序规则

package mypro01;

public class Goods {
    private int price;
    private String name;
    private int  fav;
    public Goods() {
        super();
    }
    public Goods(int price, String name, int fav) {
        super();
        this.price = price;
        this.name = name;
        this.fav = fav;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getFav() {
        return fav;
    }
    public void setFav(int fav) {
        this.fav = fav;
    }
    @Override
    public String toString() {
        return "价格:"+this.price+"点击量:"+this.fav+"商品名称:"+this.name+"\n";
    }
     
}
package mypro01;

import java.util.Comparator;

public class GoodsSorts implements Comparator<Goods>{

    @Override
    public int compare(Goods o1, Goods o2) {
        
        return -(o1.getPrice()-o2.getPrice()>0?1:(o1.getPrice()==o2.getPrice()?0:-1));
    }
    
}
package mypro01;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GoodsApp {
    public static void main(String[] args) {
        List<Goods> list =new ArrayList<Goods>();
        list.add(new Goods(100, "中国", 1000));
        list.add(new Goods(50, "日本", 32));
        list.add(new Goods(333, "英国", 23234));
        
        System.out.println("排序前:"+list);
        
        Collections.sort(list,new GoodsSorts());
        
        System.out.println("排序后:"+list);
    }
}
结果:
排序前:[价格:100点击量:1000商品名称:中国
, 价格:50点击量:32商品名称:日本
, 价格:333点击量:23234商品名称:英国
]
排序后:[价格:333点击量:23234商品名称:英国
, 价格:100点击量:1000商品名称:中国
, 价格:50点击量:32商品名称:日本
]

  

posted @ 2022-03-27 19:37  小熊尤里  阅读(301)  评论(0编辑  收藏  举报