I have created a birthday reminder app where i have data like events..
Now i want to show upcoming birthday with sorting and want to show on listview builder
void main() {
List<EventModel> events = [
EventModel(
title: 'A',
date: DateTime(1980, 08, 12),
),
EventModel(
title: 'Z',
date: DateTime(2020, 12, 28),
),
EventModel(
title: 'B',
date: DateTime(1995, 12, 21),
),
];
events.sort((a, b) => (a.date).compareTo(b.date));
for(var x in events)
print(x.date.toString());
}
above code giving me output as it is sorting entire date including year..how to sort based on month and day only..
1980-08-12 00:00:00.000
1995-12-21 00:00:00.000
2020-12-28 00:00:00.000
I need following output as upcoming birthday is like
2020-12-28 00:00:00.000
1980-08-12 00:00:00.000
1995-12-21 00:00:00.000
2
Answers
I think you can achieve your goals by simply writing a custom comparator that only compares the months and days.
Outputs
check comments of @jamesdlin its useful to solve ur problem
i have created a method named ignoreYear which return a date ignoring year and it will check whether date is before or after of today date…
here is your solution..
you might get some expert code but here is basic code…
All credit goes to jamesdlin