Tested in dartpad...
void main() {
List<Map> todos = [
{"todo": "Walk The dog", "isDone": false},
{"todo": "Complete Assignment", "isDone": true},
{"todo": "Buy Groceries", "isDone": true},
{"todo": "Gym", "isDone": false},
{"todo": "Netflix", "isDone": false},
];
todos.sort((a, b) => (a['isDone'] ? 1 : 0).compareTo(b['isDone'] ? 1 : 0));
for (var v in todos.map((e) => [e['todo'], e['isDone']])) {
print(v);
}
}
Does not deal with missing or null values.
Update: refactored mapping function out of the comparison to ensure consistency:
void main() {
List<Map> todos = [
{"todo": "Walk The dog", "isDone": false},
{"todo": "Complete Assignment", "isDone": true},
{"todo": "Buy Groceries", "isDone": true},
{"todo": "Gym", "isDone": false},
{"todo": "Netflix", "isDone": false},
];
int e2v (Map e) => e['isDone'] ? 1 : 0; // map false to 0, true to 1
todos.sort((a, b) => (e2v(a).compareTo(e2v(b))));
for (var v in todos.map((e) => [e['todo'], e['isDone']])) {
print(v);
}
}
Idea: sort should have a cousin for which the interface might look like
todos.sortBy((e) => e['isDone'] ? 1 : 0);
And watch, two hours later, I'll see how to do it with built-ins. :)
Edit: OK, 45 minutes and I've got this, but it's missing the generics:
void main() {
List<Map> todos = [
{"todo": "Walk The dog", "isDone": false},
{"todo": "Complete Assignment", "isDone": true},
{"todo": "Buy Groceries", "isDone": true},
{"todo": "Gym", "isDone": false},
{"todo": "Netflix", "isDone": false},
];
todos.sortBy((e) => e['isDone'] ? 1 : 0);
for (var v in todos.map((e) => [e['todo'], e['isDone']])) {
print(v);
}
}
extension ListSortBy on List {
void sortBy(mapper(e)) {
this.sort((a, b) => mapper(a).compareTo(mapper(b)));
}
}