sortWithAsyncKey<K extends Comparable<Object>> method

Future<void> sortWithAsyncKey<K extends Comparable<Object>>(
  1. Future<K> key(
    1. E
    )
)

A version of sortWithKey that allows the sort key to be computed asynchronously.

Implementation

Future<void> sortWithAsyncKey<K extends Comparable<Object>>(
  Future<K> Function(E) key,
) async {
  var keys = await Future.wait([for (var element in this) key(element)]);

  assert(length == keys.length);
  final keyValues = [
    for (var i = 0; i < length; i += 1) MapEntry(keys[i], this[i]),
  ]..sort(_compareKeys);

  // Copy back to mutate the original [List].
  for (var i = 0; i < length; i += 1) {
    this[i] = keyValues[i].value;
  }
}