Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

flutter dart sorting method does not work

var dataJson = response.body;
        dataJson = dataJson.replaceAll('[', '');
        dataJson = dataJson.replaceAll(']', '');
        dataJson = dataJson.replaceAll('"', '');
        var matchData = dataJson.split(',').toList();
        //matchData.sort((a, b) => a.compareTo(b)); I tested this too
        matchData.sort();

dataJson = [KR_1, KR_3, KR_4, KR_2, KR_7, KR_6, KR_5]

I want to sort the list like this :

[KR_1, KR_2, KR_3, KR_4, KR_5, KR_6, KR_7]

Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

try this;

final dataJson = ["KR_1", "KR_3", "KR_4", "KR_2", "KR_7", "KR_6", "KR_5"];
final data = dataJson.toList()..sort((a,b)=> a.compareTo(b));
log(data.toString()); // or use print

// result

[KR_1, KR_2, KR_3, KR_4, KR_5, KR_6, KR_7]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading