# Comparator的写法

If the comparator result of a is -1 then a must be "smaller" and thus gets placed closer to the head of the queue than b.

<pre class="language-java"><code class="lang-java">// 处理Integer
// smaller number first
PriorityQueue&#x3C;Integer> pq = new PriorityQueue&#x3C;>((a, b) -> a - b);
<strong>// bigger number first, use new Comparator
</strong>Queue&#x3C;Integer> pq = new PriorityQueue&#x3C;>(Comparator.reverseOrder());


// 处理Priority Queue里的HashMap
// bigger value first, use new Comparator
PriorityQueue&#x3C;Map.Entry&#x3C;Character, Integer>> pq = new PriorityQueue&#x3C;>(
    new Comparator&#x3C;Map.Entry&#x3C;Character, Integer>>() {
        @Override
        public int compare(Map.Entry&#x3C;Character, Integer> a, Map.Entry&#x3C;Character, Integer> b) {
            return b.getValue() - a.getValue();
        }
    }
);
// or
PriorityQueue&#x3C;Map.Entry&#x3C;Character, Integer>> pq = new PriorityQueue&#x3C;>(
    (a, b) -> b.getValue() - a.getValue()
);
        
// bigger value first; if the values are equal, order alphabetically
PriorityQueue&#x3C;Map.Entry&#x3C;String, Integer>> pq = new PriorityQueue&#x3C;> (
    new Comparator&#x3C;Map.Entry&#x3C;String, Integer>>() {
        @Override
        public int compare(Map.Entry&#x3C;String, Integer> a, Map.Entry&#x3C;String, Integer> b) {
            if (a.getValue() != b.getValue()) return b.getValue() - a.getValue();
            return a.getKey().compareTo(b.getKey());
        }
    }
);

// 处理Priority Queue里的int[] nums, nums里有两个数字
// order by nums[0] smallest first
// if both nums[0] values are equal, smallest nums[1] first
PriorityQueue&#x3C;int[]> pq = new PriorityQueue&#x3C;>((a,b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]);


// 用Arrays.sort处理string[]： sort from longest to shortest
Arrays.sort(strs, new Comparator&#x3C;String>() {
    public int compare(String s1, String s2) {
        return s2.length() - s1.length();
    }
});


// 用Arrays.sort处理int[][]
int[][] intervals = [[1,3],[2,6],[8,10],[15,18]]
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
// 如果a[0],b[0]相同，按照a[1],b[1]排序
Arrays.sort(intervals, (a, b) -> (a[0] != b[0]) ? a[0] - b[0] : a[1] - b[1]);
// 也可以写一个新的compare方程
Arrays.sort(intervals, new Comparator&#x3C;int[]>() {
    public int compare(int[] a, int[] b) {
        return Integer.compare(a[0], b[0]);
    }
});


// 对一个ArrayList里的一种自定义Object进行排序
// 比如这里的Object叫TimePoint, 有time和type两个变量
List&#x3C;TimePoint> list = new ArrayList&#x3C;>();
for (int query : queries) list.add(new TimePoint(query, 2)); 
// 按照time由小到大排序。如果time一样，按照type由小到大排序
list.sort((t1, t2) -> {
    if (t1.time == t2.time) return Integer.compare(t1.type, t2.type);
    else return Integer.compare(t1.time, t2.time);
});
// 类似的对ArrayList里一种自定义的Object排序
Collections.sort(intervals, new Comparator&#x3C;Interval>() {
    public int compare(Interval a, Interval b) {
        return a.start - b.start;
    }
});
// 也可以这样写
intervals.sort(
    (i1, i2)-> {return Integer.compare(i1.start, i2.start);}
);

// 对一个ArrayList里的Pair进行排序
List&#x3C;Pair&#x3C;Integer, Integer>> pairs = new ArrayList&#x3C;>();
for (int i = 0; i &#x3C; profit.length; i++) {
    pairs.add(new Pair&#x3C;Integer, Integer>(difficulty[i], profit[i]));
}
Collections.sort(pairs, Comparator.comparing(Pair::getKey));
</code></pre>
