sorry in advance if I didnt explain what I mean correctly.
Currently my issue is, I have a list List<Event> eventList
, the model of Event look like this:
class Event {
DateTime eventTime;
Event({this.eventTime});
}
and eventList looks like this:
List<Event> eventList = [
Event(eventTime: DateTime(1961, 05, 12),),
Event(eventTime: DateTime(1963, 05, 12),),
Event(eventTime: DateTime(1963, 05, 12),),
Event(eventTime: DateTime(1963, 05, 12),),
Event(eventTime: DateTime(1964, 05, 12),),
];
So there are three Event with same year 1963, what I want to is create a newList based on this eventTime, it should look more or less like this: List<List<Event>>
, so the event with same year will be grouped in a List, currently I can only sort them like this
void sortEventBasedOnTime() {
eventList.sort((a, b) {
var adate = a.eventTime;
var bdate = b.eventTime;
return adate.compareTo(
bdate);
});
}
And then I dont know how to group them, plz let me know how to solve my issue, thank you in advance!
CodePudding user response:
I tried a solution for you in DartPad. I hope it helps.
You give the new item to be added (Event) and your existing list to the addNewEvent method, and it both adds on the same one and sorts it. There may be errors as I haven't been able to fully test it.
CodePudding user response:
Thank you pskink for the inspiration, I guess this page can solve my issuesolution
CodePudding user response:
You can use .where to this.
Try this:
final myList = originalList.where((e) => e['name'] == 'sergio');
This code will create a list myList with all elements that have name "sergio".