Here you can find the source of sortMap(Map
public static <K, V> Map<K, V> sortMap(Map<K, V> map, Comparator<Entry<K, V>> compator)
//package com.java2s; //License from project: Apache License import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; public class Main { public static <K, V> Map<K, V> sortMap(Map<K, V> map, Comparator<Entry<K, V>> compator) { Map<K, V> result = new LinkedHashMap<K, V>(); List<Entry<K, V>> entries = new ArrayList<Entry<K, V>>(map.entrySet()); Collections.sort(entries, compator); for (Entry<K, V> entry : entries) { result.put(entry.getKey(), entry.getValue()); }//w w w. jav a 2s. c o m return result; } }