Refactor: inplace stable sort

This commit is contained in:
Ming Ming 2022-06-06 16:33:56 +08:00
parent 29eebe4460
commit f6614a3542
2 changed files with 8 additions and 5 deletions

View file

@ -9,11 +9,8 @@ extension IterableExtension<T> on Iterable<T> {
List<T> sorted([int Function(T a, T b)? compare]) => toList()..sort(compare);
/// Return a new stable sorted list
List<T> stableSorted([int Function(T a, T b)? compare]) {
final tmp = toList();
mergeSort(tmp, compare: compare);
return tmp;
}
List<T> 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

View file

@ -1,5 +1,7 @@
import 'dart:math' as math;
import 'package:collection/collection.dart';
extension ListExtension<T> on List<T> {
Iterable<T> takeIndex(List<int> indexes) => indexes.map((e) => this[e]);
@ -20,4 +22,8 @@ extension ListExtension<T> on List<T> {
return sublist(start, math.min(stop, length));
}
}
void stableSort([int Function(T a, T b)? compare]) {
mergeSort(this, compare: compare);
}
}