deepSortByKey method

Map<K, V> deepSortByKey()

Returns new instance of Map, which is recursively sorted by keys. It sorts by keys even Map nested as a value, nested Map inside nested Map and so on.

It does not sort nested List or Set, because keys are sorted by default in those types.

it gets rid of incomparable keys (and associated values) like:

Sort order:

  1. int and double keys
  2. String keys

Implementation

Map<K, V> deepSortByKey() => LinkedHashMap.fromIterable(
    <dynamic>[...keys.toList(growable: false).deepSort()],
    key: (k) => k,
    value: (k) {
      if (this[k] is Map) {
        return (this[k] as Map).deepSortByKey();
      } else if (this[k] is Set) {
        return _deepSortByKeyInSet(this[k] as Set);
      } else if (this[k] is List) {
        return _deepSortByKeyInList(this[k] as List);
      } else {
        return this[k];
      }
    }).cast<K, V>();