Java 7 (Android) Sort Map By Value

Aug 17, 2017
Map<String, Integer> findCountry = new HashMap<>();findCountry.put("Japan", 100);findCountry.put("Taiwan", 50);findCountry.put("Singapore", 10);findCountry.put("China", 500);List<Map.Entry<String, Integer>> sortedList = new LinkedList<>(findCountry.entrySet());Collections.sort(sortedList, new Comparator<Map.Entry<String, Integer>>() {    @Override    public int compare(Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2) {        // asc        return (e1.getValue()).compareTo(e2.getValue());        // desc        // return (e2.getValue()).compareTo(e1.getValue());    }});// need LinkedHashMap to maintain orderfindCountry = new LinkedHashMap<>();for (Map.Entry<String, Integer> entry : sortedList) {    Log.d(TAG, entry.getKey() + "=" + entry.getValue());    findCountry.put(entry.getKey(), entry.getValue());}for (Map.Entry<String, Integer> entry : findCountry.entrySet()) {    Log.d(TAG, entry.getKey() + "=" + entry.getValue());}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.