排序:將一組數據按相應的規則 排列 順序
1.規則:
基本數據類型:日常的大小排序。
引用類型:
- 內置引用類型(String,Integer..),內部已經指定規則,直接使用即可。----實現Comparable接口
1. 整數、 Integer..:根據基本數據類型大小
2. Character(字符):根據Unicode編碼順序
3. String(字符串):
1)如果其中一個是另一個起始開始的子串,返回長度之差,
2)否則返回第一個不相等的Unicode之差。
4. 日期:根據日期的長整型數比較。
- 自定義引用類型,需要按照業務規則排序。有兩種方式,分別如下所述:
當引用類型的內置排序方式無法滿足需求時可以自己實現滿足既定要求的排序,有兩種方式:
第一種: 自定義業務排序類:新建一個業務排序類實現java.util.Comparator 下的compare 接口,然后使用java提供的Collections調用排序方法,並將此業務排序類作為參數傳遞給Collections的sort方法,如下:
(1)新建一個實體類,如下


package top.wfaceboss.sort.refType2; public class Goods { // 價格 private double price; // 商品名稱 private String name; // 收藏量 private int fav; public Goods() { } public Goods(String name,double price, int fav) { super(); this.price = price; this.name = name; this.fav = fav; } public double getPrice() { return price; } public void setPrice(double 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.name + ",收藏量:" + this.fav + ",價格:" + this.price + "\n"; } }
(2)新建業務排序類(實現java.util.Comparator接口),編寫符合業務要求的排序方法,如下是按照價格排序的業務類(降序)


package top.wfaceboss.sort.refType2; /** * 按照價格排序的業務類(降序) * * @author Administrator * */ public class GoodsPriceCompare implements java.util.Comparator<Goods> { @Override public int compare(Goods o1, Goods o2) { return -(o1.getPrice()-o2.getPrice()>0?1:o1.getPrice()==o2.getPrice()?0:-1);//降序 } }
(3)使用業務排序類
package top.wfaceboss.sort.refType2; 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, 2000)); list.add(new Goods("老高視頻", 50, 2000)); list.add(new Goods("老裴視頻", 1000, 1000)); System.out.println("排序前:" + list); Collections.sort(list,new GoodsPriceCompare()); System.out.println("排序后:"+list); } }
第二種:實體類實現 java.lang.Comparable下的compareTo接口,在接口中實現滿足需求的,然后使用java提供的Collections調用排序方法sort,會自動調用此時實現的接口方法。
(1)新建一個實體類,實現java.lang.Comparable接口compareTo
,如下:


package top.wfaceboss.sort.refType; import java.text.SimpleDateFormat; import java.util.Date; /** * 新聞條目實體類 排序方式: java.lang.Comparable+compareTo * * @author Administrator * @param <T> * */ public class NewsItem implements java.lang.Comparable<NewsItem> { // 標題 private String title; // 點擊量 private int hits; // 時間 private Date pubTime; public int getHits() { return hits; } public void setHits(int hits) { this.hits = hits; } 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 Date getPubTime() { return pubTime; } public void setPubTime(Date pubTime) { this.pubTime = pubTime; } // 時間降序+點擊量升序+標題降序 @Override public int compareTo(NewsItem o) { System.out.println("============dd=========="); 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(); } }
(2)使用java自帶的Collections調用sort,對該實體類的實例進行排序:
package top.wfaceboss.sort.refType; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; /** * 使用Collections * */ public class NewsItemApp { public static void main(String[] args) { List<NewsItem> news = new ArrayList<NewsItem>(); news.add(new NewsItem("aaa", 50, new Date())); news.add(new NewsItem("bbb", 60, new Date(System.currentTimeMillis() - 1000 * 60 * 60))); news.add(new NewsItem("ccc", 30, new Date(System.currentTimeMillis() + 1000 * 60 * 60))); Collections.sort(news); System.out.println(news); } }
2.順序:
升序:從小到大
降序:從大到小
3. 排列:
算法:如冒泡...