package utils;
import java.util.*;
class ComparatorInteger implements Comparator<Integer> {
@Override //使得逆序 o1比o2小,返回正数——需要调换位置
public int compare(Integer o1, Integer o2) {
return o1 <= o2 ? 1 : -1;
}
}
class ComparatorChar implements Comparator<Character> {
@Override //使得逆序 o1比o2小(ASCII码),返回正数——需要调换位置
public int compare(Character o1, Character o2) {
return o1 <= o2 ? 1 : -1;
}
}
public class Main {
public static void main(String[] args) {
Comparator compInte = new ComparatorInteger();
Comparator compChar = new ComparatorChar();
Integer nums[] = {6, 1, 3, 5};
Arrays.sort(nums, compInte); //逆序:6 5 3 1
Character chars[] = {'c', 'a', 'b'};
Arrays.sort(chars, compChar); //逆序:c b a
}
}