Home > Software design >  flutter dart sorting method does not work
flutter dart sorting method does not work

Time:06-04

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

CodePudding user response:

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]
  • Related