Comparator
public static void main(String[] args) {
List integerList = Arrays.asList(5, 2, 6, 5, 8, 8, 4);
Comparator comparator = new Comparator() {
@Override
public int compare(Integer o1, Integer o2) {
// 升序
// 此处的 o1 对应数组 a[1] ; o2 对应数组 a[0]
// 如果 o1 > o2 即 a[1] > a[0] 依次递增,升序
if(o1 > o2){
return 1 ;
}else if(o1 < o2){
// 如果 o1 < o2 反转
return -1 ;
}
return 0;
}
};
Collections.sort(integerList,comparator);
// [2, 4, 5, 5, 6, 8, 8]
System.out.println(integerList);
Comparator comparatorAsc = new Comparator() {
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
};
List integerList1 = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
// [1, 2, 3, 4, 5, 6, 7, 8]
System.out.println(integerList1);
Collections.sort(integerList1,comparatorAsc);
// [1, 2, 3, 4, 5, 6, 7, 8]
System.out.println(integerList1);
List integerList2 = Arrays.asList(8, 7, 6, 5, 4, 3, 2, 1);
// [8, 7, 6, 5, 4, 3, 2, 1]
System.out.println(integerList2);
Collections.sort(integerList2,comparatorAsc);
// [1, 2, 3, 4, 5, 6, 7, 8]
System.out.println(integerList2);
Comparator comparatorDesc = new Comparator() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
};
List integerList3 = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
// [1, 2, 3, 4, 5, 6, 7, 8]
System.out.println(integerList3);
Collections.sort(integerList3,comparatorDesc);
// [1, 2, 3, 4, 5, 6, 7, 8]
System.out.println(integerList3);
List integerList4 = Arrays.asList(8, 7, 6, 5, 4, 3, 2, 1);
// [8, 7, 6, 5, 4, 3, 2, 1]
System.out.println(integerList4);
Collections.sort(integerList4,comparatorDesc);
// [1, 2, 3, 4, 5, 6, 7, 8]
System.out.println(integerList4);
}
// Collections - sort
public static void sort(List list, Comparator super T> c) {
Object[] a = list.toArray();
Arrays.sort(a, (Comparator)c);
ListIterator i = list.listIterator();
for (int j=0; j void sort(T[] a, Comparator super T> c) {
if (LegacyMergeSort.userRequested)
legacyMergeSort(a, c);
else
TimSort.sort(a, c);
}
// TimSort - sort
static void sort(T[] a, Comparator super T> c) {
sort(a, 0, a.length, c);
}
static void sort(T[] a, int lo, int hi, Comparator super T> c) {
if (c == null) {
Arrays.sort(a, lo, hi);
return;
}
rangeCheck(a.length, lo, hi);
int nRemaining = hi - lo;
if (nRemaining < 2)
return; // Arrays of size 0 and 1 are always sorted
// If array is small, do a "mini-TimSort" with no merges
if (nRemaining < MIN_MERGE) {
int initRunLen = countRunAndMakeAscending(a, lo, hi, c);
binarySort(a, lo, hi, lo + initRunLen, c);
return;
}
/**
* March over the array once, left to right, finding natural runs,
* extending short natural runs to minRun elements, and merging runs
* to maintain stack invariant.
*/
TimSort ts = new TimSort<>(a, c);
int minRun = minRunLength(nRemaining);
do {
// Identify next run
int runLen = countRunAndMakeAscending(a, lo, hi, c);
// If run is short, extend to min(minRun, nRemaining)
if (runLen < minRun) {
int force = nRemaining <= minRun ? nRemaining : minRun;
binarySort(a, lo, lo + force, lo + runLen, c);
runLen = force;
}
// Push run onto pending-run stack, and maybe merge
ts.pushRun(lo, runLen);
ts.mergeCollapse();
// Advance to find next run
lo += runLen;
nRemaining -= runLen;
} while (nRemaining != 0);
// Merge all remaining runs to complete sort
assert lo == hi;
ts.mergeForceCollapse();
assert ts.stackSize == 1;
}
// TimeSort - countRunAndMakeAscending
private static int countRunAndMakeAscending(T[] a, int lo, int hi,
Comparator super T> c) {
assert lo < hi;
int runHi = lo + 1;
if (runHi == hi)
return 1;
// 此处用 a[1] 与 a[0] 进行比较
// Find end of run, and reverse range if descending
if (c.compare(a[runHi++], a[lo]) < 0) { // Descending
while (runHi < hi && c.compare(a[runHi], a[runHi - 1]) < 0)
runHi++;
reverseRange(a, lo, runHi);
} else { // Ascending
while (runHi < hi && c.compare(a[runHi], a[runHi - 1]) >= 0)
runHi++;
}
return runHi - lo;
}