October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Rediscovering the Schwartzian Transform: Why a Flutter Sorting Fix Matters

A Flutter timeline fix illustrates the Schwartzian Transform: compute each expensive sort key once, sort cached key-and-item pairs, and avoid repeated parsing inside a comparator.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When a Flutter timeline sorted 10,000 activities, parsing each ISO-8601 timestamp inside the sort comparator caused enough repeated work to contribute to frame drops. Randal L. Schwartz’s fix was to parse each timestamp once, sort cached key-and-item pairs, then recover the activities: the Schwartzian Transform, often summed up as “Map → Sort → Map.”

Why sorting 10,000 timeline items caused jank

A sort comparator is called repeatedly as the algorithm compares items. Its call count can therefore be much larger than the number of items being sorted. In the Flutter incident Schwartz describes, the comparator parsed a timestamp string with DateTime.parse(activity.start) each time it needed to compare two activities. The same strings were parsed over and over.

As an Amazon Associate I earn from qualifying purchases.

The issue was not that parsing one timestamp is necessarily expensive. It was that the parsing work sat inside an operation that ran many times. For a large timeline, that repeated work contributed to frame drops; caching each parsed date before sorting removed the repeated parsing that caused the reported freeze.

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

What the Schwartzian Transform does

The transform separates key calculation from comparison. It decorates each item with its sort key, sorts those temporary pairs using the already-computed keys, and then removes the keys to return the original items.

  1. Map: calculate the derived key once for each item and pair it with that item.
  2. Sort: compare the cached keys, not the original data that requires expensive processing.
  3. Map: extract the original items in their new order.

For the timeline, the key is a parsed DateTime. Once each date has been parsed, comparisons use DateTime.compareTo rather than parsing timestamp text again.

Implementing cached-key sorting with Dart 3 records

Dart records offer a compact way to hold a temporary key and its corresponding item without defining a separate helper class:

final sorted = [
  for (final item in widget.activity)
    (key: DateTime.parse(item.start), item: item),
]..sort((a, b) => a.key.compareTo(b.key));

final result = [for (final entry in sorted) entry.item];

Records are anonymous, immutable aggregate values with named or positional fields. Dart documents them as fixed-size, heterogeneous, and typed; records require language version 3.0 or later. Here, each record keeps a parsed date and its activity together throughout the sort.

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

A reusable extension can apply the same pattern to any comparable derived key:

extension SchwartzianSortExtension<T> on Iterable<T> {
  List<T> sortedByExpensive<K extends Comparable<K>>(
    K Function(T item) keyOf,
  ) {
    final boxed = [
      for (final item in this) (key: keyOf(item), item: item),
    ]..sort((a, b) => a.key.compareTo(b.key));

    return [for (final entry in boxed) entry.item];
  }
}

This version evaluates keyOf once per input item. It returns a new list and uses additional temporary records and a decorated list; it does not mutate the original iterable.

What the reported benchmark shows—and what it does not

Schwartz reports the following results for sorting 10,000 ISO-8601 timestamp items. These are his 2026, author-reported measurements, not a universal Dart or Flutter benchmark:

Approach DateTime.parse evaluations Reported time
Naive List.sort with parsing in the comparator 215,462 186 ms
package:collection sortedBy() 127,590 107 ms
Cached-key Schwartzian implementation 10,000 14 ms

In that test, Schwartz describes the cached-key implementation as 13.3 times faster than the naive baseline and says it fit within a 60 FPS animation tick. A 60 FPS frame budget is about 16.7 ms, but a sort taking 14 ms in one test does not establish that an entire Flutter frame will meet its budget on other devices or under other workloads. Build mode, hardware, input data, allocation, and surrounding UI work all affect the result.

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

Schwartz also reports that the inspected sortedBy implementation called its key function during merge or insertion operations rather than caching one result per item. The public API documentation establishes that sortedBy and sortBy are available; it does not, by itself, establish the internal call behavior for every package version. Treat the evaluation count as specific to Schwartz’s inspected implementation and benchmark.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep equal-key ordering deterministic

Dart’s List.sort does not guarantee a stable sort: items that compare equal are not guaranteed to remain in their original relative order. That can make a timeline appear to reshuffle activities with identical timestamps.

If that matters, decorate each item with its original index as a secondary key. The index preserves input order among equal dates:

final sorted = [
  for (var i = 0; i < widget.activity.length; i++)
    (key: DateTime.parse(widget.activity[i].start), index: i,
     item: widget.activity[i]),
]..sort((a, b) {
    final byDate = a.key.compareTo(b.key);
    return byDate != 0 ? byDate : a.index.compareTo(b.index);
  });

final result = [for (final entry in sorted) entry.item];

The explicit tie-breaker makes the intended order part of the comparator instead of relying on the sorting algorithm to preserve it.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

When cached-key sorting is worth the extra allocation

The transform is most useful when both conditions apply: the collection is large enough for repeated key work to matter, and deriving the key is meaningfully more expensive than comparing it. Parsing dates is one example; Schwartz also identifies regular-expression evaluation, decoding, metadata lookup, and hashing as possible expensive key calculations.

  • Consider caching: parsing timestamp strings or computing another non-trivial key for many items, especially when the sort runs in an animation-sensitive path.
  • Prefer a simpler sort: sorting on an existing integer, DateTime, or other cheap primitive field. In that case, the temporary list and records may add allocation and copying without saving substantial CPU work.
  • Consider storing the key: if the derived value is reused elsewhere or can be maintained safely on the model, persistent caching may avoid recomputation across multiple operations. That choice adds model state and requires keeping the cached value in sync with its source data.
  • Measure in context: compare alternatives in the build and on the devices that matter to the app, and observe both elapsed time and allocation behavior.

The idea has a history beyond Dart: the decorate-sort-undecorate pattern is associated with Schwartz’s 1994 Perl-era work. In 2026, he recognized the same opportunity in a Flutter performance discussion and commented, “Almost looks like you could have used a Schwartzian Transform. :)”

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.