How do you sort a list by bool in Dart

前端 未结 1 1344
猫巷女王i
猫巷女王i 2021-01-20 10:49

How do you sort a List in dart based on a bool value, the compareTo method doesn\'t work with bool. I want true

相关标签:
1条回答
  • 2021-01-20 11:29

    You can define you 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.

    0 讨论(0)
提交回复
热议问题