/*
* Copyright 2014 Alex Bennett & Alexander Chauncey
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.demigodsrpg.util.misc;
import com.google.common.collect.Lists;
import java.util.*;
public class MapUtil2 {
/**
* Sort a Map from smallest to largest, or in <code>reverse</code>.
*
* @param map The Map object to be sorted.
* @param reverse Reverse the ordering.
* @return A sorted version of <code>map</code>.
*/
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map, boolean reverse) {
List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});
if (reverse) list = Lists.reverse(list);
Map<K, V> result = new LinkedHashMap<K, V>();
for (Map.Entry<K, V> entry : list)
result.put(entry.getKey(), entry.getValue());
return result;
}
}