nc-photos/app/lib/list_extension.dart
2022-06-07 01:39:29 +08:00

29 lines
716 B
Dart

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]);
List<T> slice(int start, [int? stop]) {
if (start < 0) {
start = math.max(length + start, 0);
}
if (stop != null && stop < 0) {
stop = math.max(length + stop, 0);
}
if (start >= length) {
return [];
} else if (stop == null) {
return sublist(start);
} else if (start >= stop) {
return [];
} else {
return sublist(start, math.min(stop, length));
}
}
void stableSort([int Function(T a, T b)? compare]) {
mergeSort(this, compare: compare);
}
}