Created
June 12, 2024 17:05
-
-
Save neilghosh/5619e09e3da17158d442546fac5157a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Sort the given array by their frequency and then by their own value | |
*/ | |
import java.io.*; | |
import java.util.*; | |
class SortByValueThenKey { | |
public static void main(String[] args) { | |
Map<Integer, Integer> ageFreq = new HashMap<>(); | |
int[] ages = {7, 1, 2, 3, 7, 1, 54, 5, 65, 7}; | |
for(int age : ages) { | |
Integer currentValue = ageFreq.putIfAbsent(Integer.valueOf(age), 1); | |
if(currentValue != null) { | |
ageFreq.put(Integer.valueOf(age), ++currentValue); | |
} | |
} | |
List<Map.Entry<Integer, Integer>> entries = new ArrayList<>(ageFreq.entrySet()); | |
entries.sort(new MapValueComparator(ageFreq)); | |
for(Map.Entry<Integer, Integer> entry : entries) { | |
System.out.println(entry.getKey()); | |
} | |
} | |
public static class MapValueComparator implements Comparator<Map.Entry<Integer, Integer>>{ | |
Map<Integer, Integer> mapToCompare; | |
public MapValueComparator(Map<Integer, Integer> mapToCompare){ | |
this.mapToCompare = mapToCompare; | |
} | |
public int compare(Map.Entry<Integer, Integer> first, Map.Entry<Integer, Integer> second ) { | |
if(first.getValue()<second.getValue()) return 1; | |
else if(second.getValue() < first.getValue()) return -1; | |
else { | |
if(first.getKey() < second.getKey()) return 1; | |
else return -1; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment