Java Sort Map by Values (ascending and descending orders)

Simple quick to use examples to sort Map by values, using Java 8 Stream APIs, in ascending and descending (reverse) orders.

In the center of logic is the method Map.Entry.comparingByValue() which compares Map.Entry in natural order on value.

Sort Map by Values using Stream APIs

In java 8, Map.Entry class has static method comparingByValue() to help you in sorting by values. This method returns a Comparator that compares Map.Entry in natural order on values.

Alternatively, you can pass a custom Comparator to use in sorting. This can be used to sort the map in reverse order.

In ascending order

Map<String, Integer> unSortedMap = getUnSortedMap();
		
System.out.println("Unsorted Map : " + unSortedMap);

//LinkedHashMap preserve the ordering of elements in which they are inserted
LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();

unSortedMap.entrySet()
	.stream()
	.sorted(Map.Entry.comparingByValue())
	.forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));

System.out.println("Sorted Map   : " + sortedMap);

Output:

Unsorted Map : {alex=1, charles=4, david=2, brian=5, elle=3}
Sorted Map   : {alex=1, david=2, elle=3, charles=4, brian=5}

In descending order

Map<String, Integer> unSortedMap = getUnSortedMap();
		
System.out.println("Unsorted Map : " + unSortedMap);

//LinkedHashMap preserve the ordering of elements in which they are inserted
LinkedHashMap<String, Integer> reverseSortedMap = new LinkedHashMap<>();

//Use Comparator.reverseOrder() for reverse ordering
unSortedMap.entrySet()
	.stream()
	.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) 
	.forEachOrdered(x -> reverseSortedMap.put(x.getKey(), x.getValue()));

System.out.println("Reverse Sorted Map   : " + reverseSortedMap);

Output:

Unsorted Map 		: {alex=1, charles=4, david=2, brian=5, elle=3}
Reverse Sorted Map  : {brian=5, charles=4, elle=3, david=2, alex=1}

Complete Example

The complete code used in this example, for your reference.

package com.howtodoinjava.spring;

import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class MapSortingExamples {

	public static void main(String[] args) {
		System.out.println("\nSorting using Java8 streams\n");
		
		sortByValueJava8Stream();
	}

	private static void sortByValueJava8Stream() 
	{
		Map<String, Integer> unSortedMap = getUnSortedMap();
		
		System.out.println("Unsorted Map : " + unSortedMap);

		LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();
		unSortedMap.entrySet().stream().sorted(Map.Entry.comparingByValue())
				.forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
		
		System.out.println("Sorted Map   : " + sortedMap);
		
		LinkedHashMap<String, Integer> reverseSortedMap = new LinkedHashMap<>();
		unSortedMap.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
				.forEachOrdered(x -> reverseSortedMap.put(x.getKey(), x.getValue()));
		
		System.out.println("Reverse Sorted Map   : " + reverseSortedMap);
	}

	private static Map<String, Integer> getUnSortedMap() 
	{
		Map<String, Integer> unsortMap = new HashMap<>();
		unsortMap.put("alex", 1);
		unsortMap.put("david", 2);
		unsortMap.put("elle", 3);
		unsortMap.put("charles", 4);
		unsortMap.put("brian", 5);
		return unsortMap;
	}
}


Output:

Sorting using Java8 streams

Unsorted Map 		: {alex=1, charles=4, david=2, brian=5, elle=3}
Sorted Map   		: {alex=1, david=2, elle=3, charles=4, brian=5}
Reverse Sorted Map  : {brian=5, charles=4, elle=3, david=2, alex=1}

Drop me your questions in comments section.

Happy Learning !!

Leave a Reply

3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial