Skip to content

Commit 0219c16

Browse files
committed
inbox: Sort once, not twice, as List.sort may not be stable
The trick of sorting first by the low-order key and then by the high-order key works great if the sort algorithm leaves elements in their existing relative order when they compare equal, aka if the sort is "stable". But Dart's List.sort disclaims any guarantee of being stable. So this trick doesn't work; the pinned streams and unpinned streams could each get scrambled out of alphabetical order in the course of the second sort. Instead do one sort, with a comparison function that looks at both criteria.
1 parent a8b084f commit 0219c16

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

lib/widgets/inbox.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,18 @@ class _InboxPageState extends State<InboxPage> with PerAccountStoreAwareStateMix
123123
&& subscriptions.containsKey(entry.key))
124124
.toList()
125125
..sort((a, b) {
126+
// TODO "pin" icon on the stream row? dividers in the list?
127+
final aPinned = subscriptions[a.key]!.pinToTop;
128+
final bPinned = subscriptions[b.key]!.pinToTop;
129+
if (aPinned != bPinned) {
130+
return aPinned ? -1 : 1;
131+
}
132+
126133
final streamA = streams[a.key]!;
127134
final streamB = streams[b.key]!;
128135

129136
// TODO(i18n) something like JS's String.prototype.localeCompare
130137
return streamA.name.toLowerCase().compareTo(streamB.name.toLowerCase());
131-
})
132-
..sort((a, b) {
133-
// TODO "pin" icon on the stream row? dividers in the list?
134-
final aPinned = subscriptions[a.key]!.pinToTop;
135-
final bPinned = subscriptions[b.key]!.pinToTop;
136-
return aPinned == bPinned ? 0 : (aPinned ? -1 : 1);
137138
});
138139

139140
for (final MapEntry(key: streamId, value: topics) in sortedUnreadStreams) {

0 commit comments

Comments
 (0)