Dart: List排序

var list = <Item>[
  Item(title: "item 1", isTopping: true),
  Item(title: "item 2"),
  Item(title: "item 3", isTopping: true),
  Item(title: "item 4"),
];

main(List<String> arguments) async {
  /// 通过排序把顶置的信息放在前面
  list.sort((a, b) => b.top.compareTo(a.top));
  for (var item in list) {
    print(item.title); // 1324
  }
}

class Item {
  Item({
    this.title = '',
    this.isTopping = false,
  });
  String title;
  bool isTopping;
  int get top => isTopping ? 1 : 0;
}

posted @ 2019-06-15 21:44  Ajanuw  阅读(1490)  评论(0编辑  收藏  举报