先上代碼:java
class ListSortExample { private String flag ; public ListSortExample(String flag){ this.flag = flag; } public String getFlag(){ return this.flag; } public void setFlag(String flag){ this.flag = flag; } } /** * 比較器,重寫compare方法,根據ListSortExample的flag字段來排序ListSortExample * */ class ComparatorListSort implements Comparator{ public int compare(Object arg0, Object arg1) { ListSortExample example0=(ListSortExample)arg0; ListSortExample example1=(ListSortExample)arg1; //首先比較年齡,若是年齡相同,則比較名字 int flag=example0.getFlag().compareTo(example1.getFlag()); return flag; } } public class TestListSort{ public static void main(String[] args){ List<ListSortExample> sortExampleList=new ArrayList<ListSortExample>(); sortExampleList.add(new ListSortExample("aa")); sortExampleList.add(new ListSortExample("ee")); sortExampleList.add(new ListSortExample("bb")); sortExampleList.add(new ListSortExample("cg")); Comparator comparator=new ComparatorListSort(); //排序--注意:sortExampleList中的對象引用必須被comparator進行比較了,便是:comparator對象的compare方法必定是對sortExampleList中的對象就行比較 Collections.sort(sortExampleList, comparator); for (ListSortExample example:sortExampleList){ System.out.println(example.getFlag()); } } }
運行結果:算法
aa數組
bb大數據
cgthis
eecode
上面初步對Collections.sort的應用作了一個簡單的描述。對象
下面的篇章咱們將會Collections.sort實現進行解析:見代碼和代碼註釋排序
public static <T> void sort(List<T> list, Comparator<? super T> c) { //將排序list轉化爲數組 Object[] a = list.toArray(); //而後調用Arrays的靜態方法sort()進行排序,因此說Collections的sort()功能是委派Arrays來實現的,咱們重點看看Arrays的sort()方法 Arrays.sort(a, (Comparator)c); //這邊簡單:將排序完成的數組對象中的數據賦值給list對象 ListIterator i = list.listIterator(); for (int j=0; j<a.length; j++) { i.next(); i.set(a[j]); } } public static <T> void sort(T[] a, Comparator<? super T> c) { //數組拷貝 T[] aux = (T[])a.clone(); if (c==null) //若是比較器爲空,採用默認比較器 mergeSort(aux, a, 0, a.length, 0); else //採用用戶自定義的比較器來比較排序 mergeSort(aux, a, 0, a.length, 0, c); } private static void mergeSort(Object[] src, Object[] dest, int low, int high, int off, Comparator c) { int length = high - low; //當數組的數據小於或者等於7個時候,直接採用冒泡排序法進行比較,而後返回 // Insertion sort on smallest arrays if (length < INSERTIONSORT_THRESHOLD) { for (int i=low; i<high; i++) for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--) swap(dest, j, j-1); return; } // Recursively sort halves of dest into src int destLow = low; int destHigh = high; low += off; high += off; int mid = (low + high) >>> 1; //若是數組中的個數大於7的話,經過調用遞歸方法,一直將原數組劃分紅多個小數組,數組的長度在7之間的,將這些小數組的中的數據按比較器中的規則排好序 //經過A,B將數組一分爲二的,而後再將1/2的數組再次劃分爲2個,直至最後被劃分的數組的長度在7以內, mergeSort(dest, src, low, mid, -off, c);----A //對src[low], src[mid](假如程爲數組1)之間的數據進行排序 mergeSort(dest, src, mid, high, -off, c);----B//對src[mid], src[high](假如程爲數組2)之間的數組進行排序 //下面的操做就是將內部已經排序好的數組1,2就行排序 // If list is already sorted, just copy from src to dest. This is an // optimization that results in faster sorts for nearly ordered lists. //當數組2的第一個數據(也就是最小數據)大於數組1的最後一個數據(就是最大數據),直接合並倆數組到到目標數組 if (c.compare(src[mid-1], src[mid]) <= 0) { System.arraycopy(src, low, dest, destLow, length); return; } // Merge sorted halves (now in src) into dest //不然採用算法,什麼算法我也忘了,很簡單的,依次比較,將比較好了的數據賦值給目標數組 for(int i = destLow, p = low, q = mid; i < destHigh; i++) { if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0) dest[i] = src[p++]; else dest[i] = src[q++]; } }
廢話再說倆句,居然要排序,顯然就要比較要排序元素的順序或者大小,而比較器的做用就是用來幹這個的。用比較器來實現對排序元素的比較。例如上面的代碼:Collections.sort(sortExampleList, comparator);comparator比較器中的compare()方法就是用來比較sortExampleList中的ListSortExample對象。遞歸
版權聲明:本文爲博主原創文章,未經博主容許不得轉載。get