skip to Main Content

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

2

Answers


  1. 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}
    ]
    
    Login or Signup to reply.
  2. 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())));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search