Question
I need to sort a Map<Key, Value> by its values in Java.
Because the values are not unique, I have been converting the keySet() into an array and sorting that array with a custom comparator that compares the values associated with each key.
Is there a simpler or more idiomatic way to sort a map by value?
For example, the goal is something like this:
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 90);
scores.put("Bob", 75);
scores.put("Charlie", 90);
scores.put("Diana", 82);
I want the entries ordered by the Integer values.
Short Answer
By the end of this page, you will understand how to sort a Java Map by its values, why maps are not automatically value-ordered, and the common patterns developers use to create a sorted result. You will also learn when to use List<Map.Entry<K,V>>, Stream, and LinkedHashMap to preserve the sorted order.
Concept
In Java, a Map stores key-value pairs, but most map implementations do not keep entries sorted by value.
Why this happens
Different Map types have different ordering rules:
HashMapdoes not guarantee any order.TreeMapsorts by key, not by value.LinkedHashMapkeeps insertion order.
So if you want a map sorted by value, you usually do not sort the map directly. Instead, you:
- Get the map entries.
- Sort the entries by value.
- Put them into an ordered structure, usually a
LinkedHashMap.
Why this matters
Sorting by value is common when you want to:
- show rankings
- display counts from highest to lowest
- sort configuration or report data by importance
- produce ordered output for users or logs
Important idea
A Map itself is not really a "sortable list." It is a lookup structure. To sort it by value, you temporarily treat its entries like a collection that can be sorted.
What about duplicate values?
Duplicate values are completely fine when sorting by value. They only matter if you need a stable tie-breaker. For example, if two entries both have value , you may also want to sort by key so the result is predictable.
Mental Model
Think of a Map like a set of labeled folders in a cabinet:
- the key is the folder label
- the value is what is written inside the folder
A cabinet like HashMap is great for quickly finding a folder by label, but it is not arranged by what is written inside each folder.
If you want folders ordered by their contents, you take all folders out, line them up on a table, sort them by the inside value, and then place them into a cabinet that keeps that new order, such as a LinkedHashMap.
So the mental model is:
Map= fast lookupList of entries= sortable viewLinkedHashMap= preserves the sorted result
Syntax and Examples
Basic approach: sort entries by value
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 90);
scores.put("Bob", 75);
scores.put("Charlie", 90);
scores.put("Diana", 82);
List<Map.Entry<String, Integer>> entries = new ArrayList<>(scores.entrySet());
entries.sort(Map.Entry.comparingByValue());
for (Map.Entry<String, Integer> entry : entries) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
What this does
entrySet()gives you all key-value pairs.new ArrayList<>(...)makes them sortable.entries.sort(...)sorts the list by value.
This is often the simplest approach if you only need sorted output.
Creating a new map that keeps the sorted order
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 90);
scores.put("Bob", 75);
scores.put("Charlie", 90);
scores.put(, );
Map<String, Integer> sorted = <>();
scores.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.forEachOrdered(entry -> sorted.put(entry.getKey(), entry.getValue()));
System.out.println(sorted);
Step by Step Execution
Consider this example:
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 90);
scores.put("Bob", 75);
scores.put("Charlie", 90);
scores.put("Diana", 82);
List<Map.Entry<String, Integer>> entries = new ArrayList<>(scores.entrySet());
entries.sort(
Map.Entry.<String, Integer>comparingByValue()
.thenComparing(Map.Entry.comparingByKey())
);
for (Map.Entry<String, Integer> entry : entries) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
Step by step
1. Create the map
The map contains these pairs:
Alice -> 90Bob -> 75Charlie -> 90Diana -> 82
Because this is a HashMap, the internal order is not guaranteed.
2. Convert entries to a list
List<Map.Entry<String, Integer>> entries = new ArrayList<>(scores.entrySet());
Real World Use Cases
Sorting a map by value appears often in real programs.
Ranking and leaderboards
Map<String, Integer> playerScores = Map.of(
"Ana", 1200,
"Ben", 950,
"Cara", 1200,
"Dan", 870
);
You may want to display highest scores first.
Counting frequencies
If you count how many times words appear, you might store:
Map<String, Integer> wordCounts = new HashMap<>();
Then sort by count to show the most common words.
Reporting and analytics
You may have category totals like:
Books -> 340Games -> 525Music -> 210
Sorting by value helps produce readable reports.
API response formatting
Sometimes backend code calculates scores, priorities, or usage metrics in a map, then sorts them before returning a response.
Configuration and diagnostics
Developers may sort metrics or error counts so the most important items appear first in logs or debug output.
Real Codebase Usage
In real projects, developers usually do not try to force a normal Map to become value-sorted forever. Instead, they use one of these patterns.
Pattern 1: Sort when presenting data
Keep data in a HashMap for fast lookup, and only sort when displaying or exporting it.
List<Map.Entry<String, Integer>> sortedEntries = new ArrayList<>(scores.entrySet());
sortedEntries.sort(Map.Entry.comparingByValue());
This is common in services and reporting code.
Pattern 2: Build a LinkedHashMap for ordered output
When the final result must behave like a map but still preserve sorted order, developers collect into a LinkedHashMap.
Map<String, Integer> sorted = scores.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.collect(
LinkedHashMap::new,
(map, entry) -> map.put(entry.getKey(), entry.getValue()),
LinkedHashMap::putAll
);
Pattern 3: Add tie-breakers for deterministic output
In production code, predictable output matters.
.sorted(
Map.Entry.<String, Integer>comparingByValue()
.thenComparing(Map.Entry.comparingByKey())
)
Without a tie-breaker, equal values may appear in an unspecified relative order depending on source ordering.
Pattern 4: Validate null handling
Common Mistakes
Mistake 1: Expecting HashMap to stay sorted
Broken idea:
Map<String, Integer> map = new HashMap<>();
// sort somehow
System.out.println(map);
A HashMap does not preserve sorted order.
Fix
Use:
- a sorted
List<Map.Entry<K,V>>, or - a
LinkedHashMapto preserve the sorted insertion order
Mistake 2: Using TreeMap to sort by value
Broken idea:
Map<String, Integer> map = new TreeMap<>();
TreeMap sorts by key, not value.
Fix
Sort entrySet() using a comparator based on getValue().
Mistake 3: Ignoring duplicate values
If multiple entries have the same value, the order among them may not be what you expect.
Comparisons
Common ways to get a map sorted by value
| Approach | What it sorts | Keeps map structure? | Preserves sorted order after sorting? | Best for |
|---|---|---|---|---|
List<Map.Entry<K,V>> + sort() | Entries | No | Yes, in the list | Displaying or iterating sorted results |
Stream + sorted() + LinkedHashMap | Entries | Yes | Yes | Returning an ordered map |
Sort keySet() using map.get(key) | Keys | No | Yes, in the key list | Works, but less direct |
Cheat Sheet
Quick reference
Sort map entries by value
List<Map.Entry<K, V>> entries = new ArrayList<>(map.entrySet());
entries.sort(Map.Entry.comparingByValue());
Sort by value descending
entries.sort(Map.Entry.<K, V>comparingByValue().reversed());
Sort by value, then key
entries.sort(
Map.Entry.<K, V>comparingByValue()
.thenComparing(Map.Entry.comparingByKey())
);
Build a LinkedHashMap with sorted order
Map<K, V> sorted = new LinkedHashMap<>();
map.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.forEachOrdered(e -> sorted.put(e.getKey(), e.getValue()));
Null-safe value sorting
entries.sort(
Comparator.comparing(
Map.Entry<K, V>::getValue,
Comparator.nullsLast(Comparator.naturalOrder())
)
);
Key rules
HashMapis not sorted.TreeMapsorts by key, not value.- To sort by value, sort
entrySet().
FAQ
Can a Java Map be sorted directly by value?
Not usually. Most Map implementations are not designed to maintain value-based order. The common solution is to sort the entries and store the result in a List or LinkedHashMap.
Should I use TreeMap to sort by value?
No. TreeMap sorts by key only.
What happens if two entries have the same value?
They can both appear in the result. If you want predictable ordering, add a secondary sort such as thenComparing(Map.Entry.comparingByKey()).
What is the easiest way to sort a map by value in Java?
A simple and common way is:
List<Map.Entry<K, V>> entries = new ArrayList<>(map.entrySet());
entries.sort(Map.Entry.comparingByValue());
How do I keep the sorted result as a map?
Insert the sorted entries into a LinkedHashMap, which preserves insertion order.
Can I sort a map by value in descending order?
Yes:
.sorted(Map.Entry.<K, V>comparingByValue().reversed())
Mini Project
Description
Create a small Java program that reads a set of product names and sales counts, then prints the products ordered by sales. This demonstrates the common real-world pattern of collecting data in a Map, sorting by value, and preserving the result in a LinkedHashMap for predictable output.
Goal
Build a program that sorts product sales by value in descending order and prints a ranked report.
Requirements
- Create a
Map<String, Integer>containing at least five products and their sales counts. - Sort the entries by sales count in descending order.
- Use the product name as a tie-breaker when sales counts are equal.
- Store the sorted result in a
LinkedHashMap. - Print the final ranking in order.
Keep learning
Related questions
Avoiding Java Code in JSP with JSP 2: EL and JSTL Explained
Learn how to avoid Java scriptlets in JSP 2 using Expression Language and JSTL, with examples, best practices, and common mistakes.
Choosing a @NotNull Annotation in Java: Validation vs Static Analysis
Learn how Java @NotNull annotations differ, when to use each one, and how to choose between validation, IDE hints, and static analysis tools.
Convert a Java Stack Trace to a String
Learn how to convert a Java exception stack trace to a string using StringWriter and PrintWriter, with examples and common mistakes.