skip to Main Content

I have a Map<String, dynamic> i get from a json response as below.

map = {
  "status": "Success",
  "payments": [
    {
      "id": 99,
      "groupID": 19,
      "paymentDueDate": "2023-05-12",
      "paymentDate": null,
      "userID": 13,
      "amount": 500,
      "forDuration": "May",
      "proofOfPayment": null,
      "paymentMode": null,
      "paymentID": null,
      "paymentToken": null,
      "approvedBy": null,
      "approvedAt": null,
      "status": "pending",
      "note": "",
      "created_at": "2023-05-09T03:39:01.000000Z",
      "updated_at": "2023-05-09T03:39:01.000000Z"
    }
  ]
};

I will like to sort by "paymentDueDate". This looks like a map of lists. Can someone help me on how i can achieve this sorting in flutter? below is what i have tried but it is not working.

 map["payments"].sort(
    (a, b) => (
      a[DateTime.tryParse('paymentDueDate')].compareTo(
        b[DateTime.tryParse('paymentDueDate')],
      ),
    ),
  );

2

Answers


  1. May be something like this

    List<dynamic> payments = map["payments"];
    payments.sort((a, b) {
      final DateTime aDate = DateTime.parse(a["paymentDueDate"]);
      final DateTime bDate = DateTime.parse(b["paymentDueDate"]);
      return aDate.compareTo(bDate);
    });
    
    Login or Signup to reply.
  2. I think you are asking for something like this.

    map["payments"].sort((a, b) {
      final DateTime aDueDate = DateTime.tryParse(a["paymentDueDate"]);
      final DateTime bDueDate = DateTime.tryParse(b["paymentDueDate"]);
    
      return aDueDate.compareTo(bDueDate);
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search