Home > database >  In Flutter how can I sort integer values stored in strings
In Flutter how can I sort integer values stored in strings

Time:12-02

I have the numbers like

 ListData=[1.69K,1.32K,1.25K,1.23K,2,45,34]

I want output like

[2,34,45,1.25K,1.32K,1.69K]

I have used

ListData.sort((a, b) => a.compareTo(b))

But It wont work

CodePudding user response:

This works:

listData.sort((a, b){
    if(a.contains('K')){
      a = a.replaceAll('K','');
      a = double.parse(a) * 1000;
    }else{
      a = double.parse(a);
    }
    if(b.contains('K')){
      b = b.replaceAll('K','');
      b = double.parse(b) * 1000;
    }else{
      b = double.parse(b);
    }
    return a.compareTo(b);
  });
  print(listData);
  • Related