VOOZH about

URL: https://www.geeksforgeeks.org/java/sort-linkedhashmap-by-values-using-comparable-interface-in-java/

⇱ Sort LinkedHashMap by Values using Comparable Interface in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sort LinkedHashMap by Values using Comparable Interface in Java

Last Updated : 23 Jul, 2025

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 


Output
LinkedHashMap without sorting : {Computer=1, Science=3, Portal=2}
LinkedHashMap after sorting : {Computer=1, Portal=2, Science=3}
Comment