Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Neither approach is universally faster. A custom comparator is usually the simpler choice when deriving the sort key is cheap. A Schwartzian transform can avoid repeatedly deriving an expensive key by computing it once per item, but it adds temporary storage and allocation work. Dart’s List.sort is also not guaranteed to preserve the order of items that compare as equal, so define tie handling explicitly when order matters.

How Dart sorting comparators work

List.sort orders a list using a comparator. The comparator must return a negative number when its first argument belongs before its second, zero when they compare as equal, or a positive number when the first belongs after the second. The API describes this as a total ordering; see the Dart Comparator API.

For a type’s natural, intrinsic order, implement Comparable and use compareTo. When the same type has multiple meaningful orderings, separate comparators are generally clearer. Dart’s Comparable documentation explains that distinction, and the Dart core library guide demonstrates sorting with fruits.sort((a, b) => a.compareTo(b));.

What each approach does

Custom comparator

A comparator can derive the key while comparing two items—for example, parsing a date string or normalizing text before comparing it. This keeps the code direct and avoids a separate decorated list. The trade-off is that sorting invokes the comparator repeatedly, so a key derived inside it may be computed again for the same item.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Schwartzian transform

A Schwartzian transform, also called decorate-sort-undecorate, computes a key for each item first, sorts records containing both the key and original item, then extracts the original items in sorted order. In Dart, the general shape is:

final decorated = items.map((item) => (key: expensiveKey(item), item: item)).toList();
decorated.sort((a, b) => a.key.compareTo(b.key));
final sortedItems = decorated.map((entry) => entry.item).toList();

This example assumes Dart records and a key type with compareTo; adapt the record and comparison to the actual key type and Dart version in use. The approach changes the work pattern: key derivation happens once per item before sorting instead of potentially recurring during comparisons. That is an algorithmic inference, not a Dart API guarantee or a measured speedup.

Which is faster in Dart?

There is no established Dart-specific benchmark result here that proves one approach is categorically faster. The answer depends on the target Dart runtime and SDK release, list size, input shape, key-derivation cost, and allocation and memory behavior. A comparator is often preferable when extracting the key is cheap; precomputation is worth testing when the key is expensive and repeated derivation is material.

Consideration Custom comparator Schwartzian transform
Key evaluations Key work performed in comparisons can recur for the same item. Computes the key once per item, then compares the stored keys.
Temporary memory and allocations Does not require a decorated list. Stores decorated entries and usually creates a result list when extracting items.
Equal-key ordering List.sort does not guarantee stable order. Also requires an explicit tie policy; decoration alone does not make the sort stable.
Clarity and maintenance Often simplest when the key is cheap or the ordering is straightforward. Useful when precomputing an expensive key makes the comparison logic clearer, but adds decoration and extraction steps.

To decide for a real workload, benchmark both versions using representative data on the deployment runtime. Keep warm-up, input regeneration, and allocation behavior consistent. Treat the result as specific to those conditions rather than as a general property of Dart sorting.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Does Dart preserve the order of items with equal keys?

No. The official List.sort API documentation says the sort function is not guaranteed to be stable. Distinct items that compare as equal may appear in either order, so do not depend on their input order surviving the sort.

Make ties deterministic with an index

If equal keys must retain their original order, attach each item’s original index and use that index as the final comparison field. For example:

final decorated = items.indexed
    .map((entry) => (key: expensiveKey(entry.$2), index: entry.$1, item: entry.$2))
    .toList();

decorated.sort((a, b) {
  final byKey = a.key.compareTo(b.key);
  return byKey != 0 ? byKey : a.index.compareTo(b.index);
});

final sortedItems = decorated.map((entry) => entry.item).toList();

The index comparison makes the intended order explicit even though List.sort itself is unstable. Alternatively, use a stable sorting strategy. The pub.dev sorted package API documents a stable merge-sort option as well as a default unstable strategy; its documentation does not establish comparative performance for this use case.

Practical choice

  • Use a custom comparator when key extraction is cheap and the comparator expresses the ordering clearly.
  • Precompute keys when key derivation is expensive enough that repeating it during comparisons may matter, and the temporary storage is acceptable.
  • Add an explicit tie-breaker or choose a stable algorithm whenever equal-key order is part of the required result.
  • Measure both approaches on representative inputs and the actual deployment runtime before making a performance claim.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.