sortedBy method
Sort the list by f
.
Returns a new list sorted by f
.
Implementation
List<T> sortedBy(dynamic Function(T) f) {
final list = <T>[];
list.addAll(this);
list.sort((a, b) {
final valueA = f(a);
final valueB = f(b);
if (valueA == null) {
if (valueB == null) {
return 0;
} else {
return 1;
}
} else if (valueB == null) {
return -1;
} else if (valueA is num) {
return valueA.compareTo(valueB);
} else if (valueA is String) {
return valueA.compareTo(valueB);
} else if (valueA is bool) {
return valueA == valueB ? 0 : (valueA ? 1 : -1);
} else if (valueA is DateTime) {
return valueA.compareTo(valueB);
} else if (valueA is Comparable) {
return valueA.compareTo(valueB);
} else {
return 0;
}
});
return list;
}