-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
multi-field-object-comparison-in-dart.dart
64 lines (56 loc) · 1.56 KB
/
multi-field-object-comparison-in-dart.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// 🐦 Twitter https://twitter.com/vandadnp
// 🔵 LinkedIn https://linkedin.com/in/vandadnp
// 🎥 YouTube https://youtube.com/c/vandadnp
// 💙 Free Flutter Course https://linktr.ee/vandadnp
// 📦 11+ Hours Bloc Course https://youtu.be/Mn254cnduOY
// 🤝 Want to support my work? https://buymeacoffee.com/vandad
import 'dart:developer' as devtools show log;
@immutable
class Reminder {
final bool isDone;
final DateTime creationDate;
const Reminder({
required this.isDone,
required this.creationDate,
});
@override
String toString() => {
'isDone': isDone,
'creationDate': creationDate.toIso8601String(),
}.toString();
}
final now = DateTime.now();
final yesterday = now.subtract(
const Duration(days: 1),
);
final reminders = [
Reminder(isDone: true, creationDate: now),
Reminder(isDone: false, creationDate: now),
Reminder(isDone: true, creationDate: yesterday),
];
extension ToInt on bool {
int toInt() => this ? 1 : 0;
}
extension ReminderSorting on Iterable<Reminder> {
List<Reminder> sorted() => [...this]..sort(
(lhs, rhs) {
final isDone = lhs.isDone.toInt().compareTo(
rhs.isDone.toInt(),
);
if (isDone != 0) {
return isDone;
}
return lhs.creationDate.compareTo(
rhs.creationDate,
);
},
);
}
extension Log on Object {
void log() => devtools.log(toString());
}
void testIt() {
reminders.log();
final sorted = reminders.sorted();
sorted.log();
}