您的位置:首页 > 编程语言 > Java开发

DAY13:尚学堂高琪JAVA(124~126)自定义类型的排序

2019-03-09 20:01 106 查看

Date类型的排序:整体与上一节冒泡排序一致,只需注意时间类型的数据比较不能直接相互减,要用.compareTo方法
DateSort.java

package sort;
//124 Date排序
import java.util.Arrays;
import java.util.Date;
public class DateSort {
public static void main(String[] args) {
Date[] arr=new Date[3];
arr[0]=new Date();
arr[1]=new Date(System.currentTimeMillis()-1000*60*60);
arr[2]=new Date(System.currentTimeMillis()+1000*60*60);
sort(arr);
Utils.sort(arr);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void sort(Date[] arr) {
for (int j = 0; j < arr.length - 1; j++) {
boolean sorted = true;
for (int i = 0; i < arr.length - 1 - j; i++) {
if (((Comparable)(arr[i])).compareTo( arr[i + 1] )>0 ) {
Date temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
sorted = false;
}
}
if (sorted) {
break;
}
}
System.out.println(Arrays.toString(arr));
}
}

自定义类型的排序
方法一:实体类实现java.lang.Comparable并重写compareTo方法
方法二:提供额外的业务排序类,让其实现java.lang.Comparator并重写compare方法
实例一:

package sort;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.omg.Messaging.SyncScopeHelper;
import sort.Utils;
//126 引用类型的排序
public class NewsItemApp {
public static void main(String[] args) {
List<NewsItem> news=new ArrayList<NewsItem>();
news.add(new NewsItem("bbbbb",50,new Date(System.currentTimeMillis()-60*60*1000)));
news.add(new NewsItem("aaaaa",100,new Date()));
news.add(new NewsItem("ccccc",60,new Date(System.currentTimeMillis()-60*60*1000)));
// TODO Auto-generated method stub
System.out.println("排序前:"+news);
Collections.sort(news);
System.out.println("排序后:"+news);
}
}
package sort;
//126 引用类型的排序
import java.text.SimpleDateFormat;
import java.util.Date;
public class NewsItem implements java.lang.Comparable<NewsItem>{
private String title;
private int hits;
private Date publictime;
public NewsItem() {
}
public NewsItem(String title, int hits, Date publictime) {
super();
this.title = title;
this.hits = hits;
this.publictime = publictime;
}
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 getPublictime() {
return publictime;
}
public void setPublictime(Date publictime) {
this.publictime = publictime;
}
//时间降序,点击量升序,标题降序
@Override
public int compareTo(NewsItem o) {
int result=0;
result=-this.publictime.compareTo(o.publictime);
if (result==0) {
result=this.hits-o.hits;
if (result==0) {
result=-this.title.compareTo(o.title);
}
}
return result;
}
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append("标题:").append(this.title);
sb.append("\t时间:").append(new SimpleDateFormat("YYYY-MM-dd HH:MM:SS").format(this.publictime));
sb.append("\t点击量:").append(this.hits).append("\n");
return sb.toString();
}
}

实例二:
GoodsApp.java

package sort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class GoodsApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Goods> list=new ArrayList<Goods>();
list.add(new Goods("Aava入门",50,1000));
list.add(new Goods("Dava进阶",60,900));
list.add(new Goods("Cava实战",70,5000));
System.out.println("排序前:"+list);
//Collections.sort(list,new GoodsPriceComp());
Collections.sort(list,new GoodsNameComp());
System.out.println("排序前:"+list);
}
}

Goods.java

package sort;
/*126 实体类
* */
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;
}
public String toString(){
return "商品名:   "+this.getName()+"价格:     "+this.getPrice() +"收藏量:    "+this.fav+"\n";
}
}

额外的业务排序类:
GoodsPriceComp.java

package sort;
/*
* 按照价格排序的业务类(降序)*/
public class GoodsPriceComp implements java.util.Comparator<Goods> {
public static void main(String[] args) {
}
@Override
public int compare(Goods o1, Goods o2) {
return -o1.getPrice()-o2.getPrice()>0?1:(o1.getPrice()-o2.getPrice()==0?0:-1);
}
}

GoodsNameComp.java

package sort;
public class GoodsNameComp implements java.util.Comparator<Goods>{
public static void main(String[] args) {
}
@Override
public int compare(Goods o1, Goods o2) {
// TODO Auto-generated method stub
int temp=o1.getName().compareTo(o2.getName());
return temp>0?1:(temp==0?0:-1);
}
}

推荐用第二种,优势:
1.解耦:与实体类分开
2.方便:应对多变的排序规则

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: