sortByKey method

void sortByKey(
  1. K key, {
  2. Comparator<V>? compare,
})

Sorts this List<Map>> by key.

  • compare the optional key comparator.

Implementation

void sortByKey(K key, {Comparator<V>? compare}) {
  var comparator =
      compare ?? ((dynamic a, dynamic b) => a.compareTo(b) as int);

  sort((m1, m2) {
    var v1 = m1[key];
    var v2 = m2[key];
    if (v1 == null && v2 == null) {
      return 0;
    } else if (v1 == null) {
      return 1;
    } else if (v2 == null) {
      return -1;
    } else {
      return comparator(v1, v2);
    }
  });
}