Home > OS >  How to filter DateTime(intl) in flutter?
How to filter DateTime(intl) in flutter?

Time:12-14

I have list of data and I want to filter them from earliest to lates

The list with data looks like this:

[{id: 73, startTime: 2022-12-13T15:30:57.244Z}, {id: 74, startTime: 2022-12-13T10:00:57.244Z}];

And the output should look like this:

[{id: 74, startTime: 2022-12-13T10:00:57.244Z}, {id: 73, startTime: 2022-12-13T15:30:57.244Z}];

How can I do this? P.S. I am using intl

CodePudding user response:

Lets say we have this list:

List dateList = [
    {"id": 73, "startTime": "2022-12-13T15:30:57.244Z"},
    {"id": 74, "startTime": "2022-12-13T10:00:57.244Z"},
  ];

you can use sort() like this:

dateList.sort((a, b) => a["startTime"].compareTo(b["startTime"]));

result:

[
   {id: 74, startTime: 2022-12-13T10:00:57.244Z},
   {id: 73, startTime: 2022-12-13T15:30:57.244Z}
]

CodePudding user response:

You could use something like:

 var parsedList = [
    {'id': 73, 'startTime': '2022-12-13T15:30:57.244Z'},
    {'id': 74, 'startTime': '2022-12-13T10:00:57.244Z'}
  ];

  parsedList.sort((a, b) => DateTime.parse(a['startTime'].toString())
      .compareTo(DateTime.parse(b['startTime'].toString())));
  • Related