Sort LinkedHashMap by Values using Comparable Interface in Java Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. Assuming you have gone through LinkedHashMap in java and know about LinkedHashMap. Syntax: int compare(T obj) ; Illustration: Input : { GEEKS=1, geeks=3, for=2 } Output : { GEEKS=1, for=2, geeks=3 } Input : { 101 = 2, 102 = 9, 103 = 1, 105 = 7 } Output : { 103 = 1, 101 = 2, 105 = 7, 102 = 9 } Method: While using Comparable interface to sort LinkedHashMap by value, this interface imposes a total ordering on the objects of each class that implements it. This ordering refers to as the class's natural ordering and the class's comparator method is referred to as its natural comparison method. Implementation: Example Java // Java program to sort the values of LinkedHashMap // Importing required classes from // java.util package import java.util.*; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map.*; // Class public class GFG { // Main driver method public static void main(String[] args) { // Creating an LinkedHashMap object LinkedHashMap<String, Integer> l_map = new LinkedHashMap<String, Integer>(); // Adding element to LinkedHashSet // Custom inputs l_map.put("Computer", 1); l_map.put("Science", 3); l_map.put("Portal", 2); // Display message System.out.print( "LinkedHashMap without sorting : "); // Print the elements of Map in above object // just after addition without sorting System.out.println(l_map); // Convert key-value from the LinkedHashMap to List // using entryset() method List<Map.Entry<String, Integer> > list = new ArrayList<Map.Entry<String, Integer> >( l_map.entrySet()); // Comparable Interface function to // sort the values of List Collections.sort( list, new Comparator<Map.Entry<String, Integer> >() { // Comparing entries public int compare( Entry<String, Integer> entry1, Entry<String, Integer> entry2) { return entry1.getValue() - entry2.getValue(); } }); // Clear the above LinkedHashMap // using clear() method l_map.clear(); // Iterating over elements using for each loop for (Map.Entry<String, Integer> entry : list) { // Put all sorted value back to the // LinkedHashMap l_map.put(entry.getKey(), entry.getValue()); } // Display and print // the sorted value of LinkedHashMap System.out.println( "LinkedHashMap after sorting : " + l_map); } } OutputLinkedHashMap without sorting : {Computer=1, Science=3, Portal=2} LinkedHashMap after sorting : {Computer=1, Portal=2, Science=3} Comment More infoAdvertise with us A aksrathod07 Follow Improve Article Tags : Java Technical Scripter Java Programs Java-Collections Java-LinkedHashMap Java-Comparable +2 More Practice Tags : JavaJava-Collections Explore BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOPs & InterfacesClasses and Objects in Java10 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial15+ min readSynchronization in Java10 min readFile Handling in Java5 min readJava Method References9 min readJava 8 Stream Tutorial15+ min readJava Networking15+ min readJDBC Tutorial12 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions7 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like