Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you sort a list by bool in Dart

Tags:

flutter

dart

How do you sort a List in dart based on a bool value, the compareTo method doesn't work with bool. I want true values to appear at the top of the list.

like image 710
John Avatar asked May 26 '20 20:05

John


1 Answers

You can define your own compare function for bool and pass it to the sort method of List.

Example with booleans as your bool List:

booleans.sort((a, b) {
  if(b) {
    return 1;
  }
  return -1;
});

This example tells the sort method that true elements should be sorted higher than false elements.

like image 162
Christopher Moore Avatar answered Nov 05 '22 18:11

Christopher Moore