Skip to content

Instantly share code, notes, and snippets.

@neilghosh
Created June 12, 2024 17:05
Show Gist options
  • Save neilghosh/5619e09e3da17158d442546fac5157a8 to your computer and use it in GitHub Desktop.
Save neilghosh/5619e09e3da17158d442546fac5157a8 to your computer and use it in GitHub Desktop.
/*
* 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