sortLike method

L<T> sortLike(
  1. Iterable<T> ordering
)

Sorts this list according to the order specified by the ordering iterable. Items which don't appear in ordering will be included in the end, in no particular order.

Note: Not very efficient at the moment (will be improved in the future). Please use for a small number of items.

Implementation

L<T> sortLike(Iterable<T> ordering) {
  Set<T> orderingSet = Set.of(ordering);
  Set<T> newSet = Set.of(this);
  Set<T> intersection = orderingSet.intersection(newSet);
  Set<T> difference = newSet.difference(orderingSet);
  List<T> result = ordering.where((element) => intersection.contains(element)).toList();
  result.addAll(difference);
  return LFlat<T>.unsafe(result);
}