From f6614a3542c7fe0db46adfe8d93f73e162c0d95f Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Mon, 6 Jun 2022 16:33:56 +0800 Subject: [PATCH] Refactor: inplace stable sort --- app/lib/iterable_extension.dart | 7 ++----- app/lib/list_extension.dart | 6 ++++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/lib/iterable_extension.dart b/app/lib/iterable_extension.dart index 597d95f5..38a1e059 100644 --- a/app/lib/iterable_extension.dart +++ b/app/lib/iterable_extension.dart @@ -9,11 +9,8 @@ extension IterableExtension on Iterable { List sorted([int Function(T a, T b)? compare]) => toList()..sort(compare); /// Return a new stable sorted list - List stableSorted([int Function(T a, T b)? compare]) { - final tmp = toList(); - mergeSort(tmp, compare: compare); - return tmp; - } + List stableSorted([int Function(T a, T b)? compare]) => + toList()..stableSort(compare); /// Return a string representation of this iterable by joining the result of /// toString for each items diff --git a/app/lib/list_extension.dart b/app/lib/list_extension.dart index f9d4d764..4d5bb0fb 100644 --- a/app/lib/list_extension.dart +++ b/app/lib/list_extension.dart @@ -1,5 +1,7 @@ import 'dart:math' as math; +import 'package:collection/collection.dart'; + extension ListExtension on List { Iterable takeIndex(List indexes) => indexes.map((e) => this[e]); @@ -20,4 +22,8 @@ extension ListExtension on List { return sublist(start, math.min(stop, length)); } } + + void stableSort([int Function(T a, T b)? compare]) { + mergeSort(this, compare: compare); + } }