How do you sort a list by bool in Dart

混江龙づ霸主 提交于 2020-11-30 00:38:14

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/62031301/how-do-you-sort-a-list-by-bool-in-dart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!