简体   繁体   中英

How to sort a list of integers in dart based on their face values?

I have tried this

void main() {
  final List<String> values= ["1","1000","10","100","200","101","0","100500"];
  values.cast<int>();//change type of list
  values.sort();//sorting function
  print(strikes);//printing output
}

in dartpad, and output is like [0, 1, 10, 100, 1000, 100500, 101, 200]
but I want [0, 1, 10, 100, 101, 200, 1000, 100500]

Any help is appreciated.

Program below gives the desired output,
by first parsing each string into integer and getting a list of integers,
then sorting that list of integer and storing it in a variable for further usage.

void main() {

  //List to process
  final List<String> values= ["1","1000","10","100","200","101","0","100500"];

  //sorting and storing the above list
  final sorted = values.map(int.parse).toList()..sort();

  //printing the output
  print(sorted);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM