Skip to content

Latest commit

 

History

History
11 lines (8 loc) · 414 Bytes

sorting-map-entries-via-value.md

File metadata and controls

11 lines (8 loc) · 414 Bytes

Sorting Map entries via value

Sometimes you'll have a map and need to sort the keys so that they are ordered by the value of corresponding their value, with Java 8 it is super simple to achieve:

		Map<String, Integer> sortedMap = map.entrySet()
				.stream()
				.sorted(reverseOrder(comparingByValue()))
				.collect(toMap(Entry::getKey, Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));