public static <K, V> Map<K, V> toSorted(Map<K, V> self, Comparator<Map.Entry<K, V>> comparator) {
        List<Map.Entry<K, V>> sortedEntries = toSorted(self.entrySet(), comparator);
        Map<K, V> result = new LinkedHashMap<K, V>();
        for (Map.Entry<K, V> entry : sortedEntries) {
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }
public static < K , V > Map < K , V > toSorted ( Map < K , V > self , Comparator < Map . Entry < K , V > > comparator ) { List < Map . Entry < K , V > > sortedEntries = toSorted ( self . entrySet ( ) , comparator ) ; Map < K , V > result = new LinkedHashMap < K , V > ( ) ; for ( Map . Entry < K , V > entry : sortedEntries ) { result . put ( entry . getKey ( ) , entry . getValue ( ) ) ; } return result ; }
Sorts the elements from the given map into a new ordered map using
the supplied comparator to determine the ordering. The original map is unchanged.
<pre class="groovyTestCase">
def keyComparator = [compare: { e1, e2 {@code ->} e1.key {@code <=>} e2.key }] as Comparator
def valueComparator = [compare: { e1, e2 {@code ->} e1.value {@code <=>} e2.value }] as Comparator
def map1 = [a:5, b:3, d:4, c:6].toSorted(keyComparator)
assert map1.toString() == '[a:5, b:3, c:6, d:4]'
def map2 = [a:5, b:3, d:4, c:6].toSorted(valueComparator)
assert map2.toString() == '[b:3, d:4, a:5, c:6]'
</pre>

@param self the original unsorted map
@param comparator a Comparator used for the comparison
@return the sorted map
@since 2.4.0