public static < K , V > Map < K , V > sort ( Map < K , V > map , Comparator < Map . Entry < K , V > > comparator ) { Assert . notNull ( map , "Missing map!" ) ; Assert . notNull ( comparator , "Missing comparator!" ) ; List < Map . Entry < K , V > > list = new LinkedList <> ( map . entrySet ( ) ) ; Collections . sort ( list , comparator ) ; Map < K , V > result = new LinkedHashMap <> ( ) ; for ( Map . Entry < K , V > entry : list ) { result . put ( entry . getKey ( ) , entry . getValue ( ) ) ; } return result ; }
Sorts map with comparator