数字が大きい順
void sort() {
final List<Map<String,dynamic>> list = [
{'score': 1},
{'score': 2},
{'score': 3}
];
list.sort((a,b) => b['score'].compareTo(a['score']));
print(list);
}
結果
[{score: 3}, {score: 2}, {score: 1}]
数字が小さい順
void sort(){
final List<Map<String,dynamic>> list = [
{'score': 1},
{'score': 2},
{'score': 3}
];
list.sort((a,b) => a['score'].compareTo(b['score']));
print(list);
}
結果
[{score: 1}, {score: 2}, {score: 3}]
新しい順
void sort(){
final firstDay = DateTime(2021,10,10);
final nextDay = DateTime(2021,10,11);
final nextNextDay = DateTime(2021,10,12);
final List<Map<String,dynamic>> timeStamps = [
{'createdAt': firstDay},
{'createdAt': nextDay},
{'createdAt': nextNextDay}
];
timeStamps.sort((a,b) => b['createdAt'].compareTo(a['createdAt']));
print(timeStamps);
}
結果
[{createdAt: 2021-10-12 00:00:00.000}, {createdAt: 2021-10-11 00:00:00.000}, {createdAt: 2021-10-10 00:00:00.000}]
古い順
void sort(){
final firstDay = DateTime(2021,10,10);
final nextDay = DateTime(2021,10,11);
final nextNextDay = DateTime(2021,10,12);
final List<Map<String,dynamic>> timeStamps = [
{'createdAt': firstDay},
{'createdAt': nextDay},
{'createdAt': nextNextDay}
];
timeStamps.sort((a,b) => a['createdAt'].compareTo(b['createdAt']));
print(timeStamps);
}
結果
[{createdAt: 2021-10-10 00:00:00.000}, {createdAt: 2021-10-11 00:00:00.000}, {createdAt: 2021-10-12 00:00:00.000}]