0

I have this data and I want to sort all the type_description value ascending. How can I do this?

data = [
  {
    'name': 'asd',
    'contact': [{
        "country_code": "1",
        "area_code": null,
        "contact_no": "4616984351",
        "email": null,
        "type_description": "Mobile - Primary"
      },
      {
        "country_code": null,
        "area_code": "416",
        "contact_no": "6984351",
        "email": null,
        "type_description": "Landline - Business"
      },
      {
        "country_code": null,
        "area_code": null,
        "contact_no": null,
        "email": "[email protected]",
        "type_description": "Email - Primary"
      }
    ]
  },
  {
    'name': 'dsa',
    'contact': [{
        "country_code": "1",
        "area_code": null,
        "contact_no": "4616984351",
        "email": null,
        "type_description": "Mobile - Primary"
      },
      {
        "country_code": null,
        "area_code": "416",
        "contact_no": "6984351",
        "email": null,
        "type_description": "Landline - Business"
      },
      {
        "country_code": null,
        "area_code": null,
        "contact_no": null,
        "email": "[email protected]",
        "type_description": "Email - Primary"
      }
    ]
  }
];

I'm expecting the data to be look like this

data = [
  {
    'name': 'asd',
    'contact': [{
        "country_code": null,
        "area_code": null,
        "contact_no": null,
        "email": "[email protected]",
        "type_description": "Email - Primary"
      },
      {
        "country_code": null,
        "area_code": "416",
        "contact_no": "6984351",
        "email": null,
        "type_description": "Landline - Business"
      },
      {
        "country_code": "1",
        "area_code": null,
        "contact_no": "4616984351",
        "email": null,
        "type_description": "Mobile - Primary"
      },
    ]
  },
  {
    'name': 'dsa',
    'contact': [{
        "country_code": null,
        "area_code": null,
        "contact_no": null,
        "email": "[email protected]",
        "type_description": "Email - Primary"
      },
      {
        "country_code": null,
        "area_code": "416",
        "contact_no": "6984351",
        "email": null,
        "type_description": "Landline - Business"
      },
      {
        "country_code": "1",
        "area_code": null,
        "contact_no": "4616984351",
        "email": null,
        "type_description": "Mobile - Primary"
      },
    ]
  }
];

1 Answer 1

1

do insert the jsondata in the variable name datastore

List<Map<String,dynamic>> dataStrore = [put all json data here];

Now do the sort

dataStore.forEach((user){
List<Map<String, dynamic>> contact = user['contact'];
contact.sort((a,b)) => a['type_discription'].compareTo(b['typediscription'])));
})

Now print and see the sorted data

print(dataStore)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.