sortedMerge method

Iterable<E> sortedMerge(
  1. Iterable<E> other,
  2. int compareTo(
    1. E a,
    2. E b
    )
)

Merges this iterable with other using compareTo to determine the order.

Implementation

Iterable<E> sortedMerge(
  Iterable<E> other,
  int Function(E a, E b) compareTo,
) sync* {
  final Iterator<E> iterator = this.iterator;
  final Iterator<E> otherIterator = other.iterator;
  bool hasNext = iterator.moveNext();
  bool otherHasNext = otherIterator.moveNext();
  while (hasNext && otherHasNext) {
    final E current = iterator.current;
    final E otherCurrent = otherIterator.current;
    final int comparison = compareTo(current, otherCurrent);
    if (comparison < 0) {
      yield current;
      hasNext = iterator.moveNext();
    } else if (comparison > 0) {
      yield otherCurrent;
      otherHasNext = otherIterator.moveNext();
    } else {
      yield current;
      hasNext = iterator.moveNext();
      otherHasNext = otherIterator.moveNext();
    }
  }
  while (hasNext) {
    yield iterator.current;
    hasNext = iterator.moveNext();
  }
  while (otherHasNext) {
    yield otherIterator.current;
    otherHasNext = otherIterator.moveNext();
  }
}