thenBy method

Query<T> thenBy(
  1. Comparable keyFn(
    1. CItem<T> item
    ), {
  2. bool descending = false,
})

Adds a secondary (tiebreaker) sort key. Multiple thenBy calls accumulate, so:

q.orderBy((t) => t.obj.dueDate)
 .thenBy((t) => t.obj.title, descending: true)
 .thenBy((t) => t.createdAt);

orders by dueDate, then within ties by title descending, then within still-ties by createdAt. Each level has its own independent descending.

Throws StateError when called without a prior orderBy — thenBy is a tiebreaker by definition, so it has nothing to tiebreak against on its own.

Implementation

Query<T> thenBy(
  Comparable<dynamic> Function(CItem<T> item) keyFn, {
  bool descending = false,
}) {
  if (_spec.orderBys.isEmpty) {
    throw StateError(
      'thenBy() requires a prior orderBy(). Call .orderBy(...) first '
      'to establish the primary sort, then chain .thenBy(...) for '
      'tiebreakers.',
    );
  }
  return Query<T>._(
    _collection,
    _spec._withOrderBys([
      ..._spec.orderBys,
      _OrderBy<T>(keyFn, descending: descending),
    ]),
  );
}