From 1a5fc2bbe2a729eb499340223854317c7ddcf9ac Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Fri, 9 Jul 2021 02:35:32 +0800 Subject: [PATCH] Use stable sort for album --- lib/entity/album/sort_provider.dart | 2 +- lib/iterable_extension.dart | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/entity/album/sort_provider.dart b/lib/entity/album/sort_provider.dart index 6ccba036..ddc56ef1 100644 --- a/lib/entity/album/sort_provider.dart +++ b/lib/entity/album/sort_provider.dart @@ -137,7 +137,7 @@ class AlbumTimeSortProvider extends AlbumReversibleSortProvider { return Tuple2( prevFileTime ?? DateTime.fromMillisecondsSinceEpoch(0), e); }) - .sorted((x, y) { + .stableSorted((x, y) { if (isAscending) { return x.item1.compareTo(y.item1); } else { diff --git a/lib/iterable_extension.dart b/lib/iterable_extension.dart index 09cb68c5..f6b0c91e 100644 --- a/lib/iterable_extension.dart +++ b/lib/iterable_extension.dart @@ -1,9 +1,17 @@ +import 'package:flutter/foundation.dart'; import 'package:tuple/tuple.dart'; extension IterableExtension on Iterable { /// Return a new sorted list List sorted([int compare(T a, T b)]) => this.toList()..sort(compare); + /// Return a new stable sorted list + List stableSorted([int compare(T a, T b)]) { + final tmp = this.toList(); + mergeSort(tmp, compare: compare); + return tmp; + } + /// Return a string representation of this iterable by joining the result of /// toString for each items String toReadableString() => "[${join(', ')}]";