package com.gamingmesh.jobs.stuff; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import com.gamingmesh.jobs.container.LogAmounts; public class Sorting { public static Map sortDESC(Map unsortMap) { // Convert Map to List List> list = new LinkedList>(unsortMap.entrySet()); // Sort list with comparator, to compare the Map values Collections.sort(list, new Comparator>() { @Override public int compare(Map.Entry o1, Map.Entry o2) { return (o2.getValue()).compareTo(o1.getValue()); } }); // Convert sorted map back to a Map Map sortedMap = new LinkedHashMap(); for (Iterator> it = list.iterator(); it.hasNext();) { Map.Entry entry = it.next(); sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; } public static Map sortDoubleDESC(Map unsortMap) { // Convert Map to List List> list = new LinkedList>(unsortMap.entrySet()); // Sort list with comparator, to compare the Map values Collections.sort(list, new Comparator>() { @Override public int compare(Map.Entry o1, Map.Entry o2) { return (o2.getValue()).compareTo(o1.getValue()); } }); // Convert sorted map back to a Map Map sortedMap = new LinkedHashMap(); for (Iterator> it = list.iterator(); it.hasNext();) { Map.Entry entry = it.next(); sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; } public static Map sortDoubleDESCByLog(Map unsortMap) { // Convert Map to List List> list = new LinkedList>(unsortMap.entrySet()); // Sort list with comparator, to compare the Map values Collections.sort(list, new Comparator>() { @Override public int compare(Map.Entry o1, Map.Entry o2) { return (o2.getValue()).compareTo(o1.getValue()); } }); // Convert sorted map back to a Map Map sortedMap = new LinkedHashMap(); for (Iterator> it = list.iterator(); it.hasNext();) { Map.Entry entry = it.next(); sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; } public static Map sortASC(Map unsortMap) { // Convert Map to List List> list = new LinkedList>(unsortMap.entrySet()); // Sort list with comparator, to compare the Map values Collections.sort(list, new Comparator>() { @Override public int compare(Map.Entry o1, Map.Entry o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // Convert sorted map back to a Map Map sortedMap = new LinkedHashMap(); for (Iterator> it = list.iterator(); it.hasNext();) { Map.Entry entry = it.next(); sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; } }