Here you can find the source of sort(T map, Comparator
public static <K, V, T extends Map<K, V>> T sort(T map, Comparator<Map.Entry<K, V>> c)
//package com.java2s; //License from project: Open Source License import java.util.Collections; import java.util.Comparator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; public class Main { public static <K, V, T extends Map<K, V>> T sort(T map, Comparator<Map.Entry<K, V>> c) { List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet()); Collections.sort(list, c); Object result = new LinkedHashMap<>(); for (Map.Entry<K, V> entry : list) { ((T) result).put(entry.getKey(), entry.getValue()); }/*w ww . j ava2s .com*/ return (T) result; } }