[Java] Sort using custom (frequency) comparator
Calculate the occurence of each number and store it in a hashmap\n2. Define a custom comparator to sort first based on frequency and then based on value\n3. Fil
1. Calculate the occurence of each number and store it in a hashmap\n2. Define a custom comparator to sort first based on frequency and then based on value\n3. Fill into the result array\n\n```\npublic int[] frequencySort(int[] nums) {\n        Map<Integer, Integer> freqCount = new HashMap();\n        for (int n : nums) {\n            freqCount.put(n, freqCount.getOrDefault(n, 0) + 1);\n        }\n        Comparator<Map.Entry<Integer, Integer>> valueComparator = new Comparator<Map.Entry<Integer, Integer>>() {\n            @Override\n            public int compare(Map.Entry<Integer, Integer> e1, Map.Entry<Integer, Integer> e2) {\n                return e1.getValue() == e2.getValue() ? (e1.getKey() < e2.getKey() ? 1 : -1) : (e1.getValue() < e2.getValue() ? -1 : 1);\n            }\n        };\n        \n        List<Map.Entry<Integer, Integer>> sortedList = new ArrayList<Map.Entry<Integer, Integer>>(freqCount.entrySet());\n        Collections.sort(sortedList, valueComparator);\n        int[] result = new int[nums.length];\n        int i = 0;\n        for (Map.Entry<Integer, Integer> e : sortedList) {\n            for (int j = 0; j < e.getValue(); ++j) {\n                result[i++] = e.getKey();\n            }\n        }\n        return result;\n    }\n```
['Java']