# Sorting a Map by Values [TOC] ###### tags: `java` --- ## Using `sort()` Method ### In Ascending Order ```java Map<Integer, Integer> map = new HashMap<>(); // omitted map population for brevity List<Entry<Integer, Integer>> list = new ArrayList<>( map.entrySet() ); list.sort( Entry.comparingByValue() ); ``` ### In Descending Order ```java Map<Integer, Integer> map = new HashMap<>(); // omitted map population for brevity list.sort( Entry.comparingByValue( Comparator.reverseOrder() ) ); ``` ## Using `sorted()` Method ### In Ascending Order ```java Map<Integer, Integer> map = new HashMap<>(); // omitted map population for brevity Stream<Map.Entry<Integer, Integer>> sorted = map.entrySet().stream().sorted( Map.Entry.comparingByValue() ); ``` ### In Descending Order ```java Map<Integer, Integer> map = new HashMap<>(); // omitted map population for brevity Stream<Map.Entry<Integer, Integer>> sorted = map.entrySet().stream().sorted( Map.Entry.comparingByValue( Comparator.reverseOrder() ) ); ``` ## Source - [Linux Hint LLC](https://linuxhint.com/) - [**How to Sort a Map by Value in Java**](https://linuxhint.com/sort-map-by-value-in-java/)