orderBy method

Query<T> orderBy(
  1. Object field, {
  2. bool descending = false,
})

Creates and returns a new Query that's additionally sorted by field, a String (dots separate nested fields; throws an ArgumentError for an invalid path) or a FieldPath (FieldPath.documentId sorts by document id), in ascending order or, with descending, in descending order.

Each call adds a sort key after the previous ones; documents that are equal on every key are ordered by their id. A document that does not have field is not in the results. Call orderBy before the cursor methods (startAt and the others): it throws an ArgumentError when the query already has a cursor.

final QuerySnapshot<Map<String, Object?>> snapshot = await firestore
    .collection('cities')
    .orderBy('state')
    .orderBy('population', descending: true)
    .get();

Implementation

Query<T> orderBy(Object field, {bool descending = false}) {
  final FieldPath fieldPath = _fieldPathOf('orderBy', field);
  return _withQuery(
    _builder.orderBy(query, fieldPath.internalPath, descending: descending),
  );
}