Java tutorial
//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 { /** * map sort * @param map * @param compator * @return */ 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()); } return result; } }