颤抖地找出价格最高的ToDoItem的标题

问题描述

待办事项

class TodoItem{
  final String id;
  final String title;
  final Color color;
  final int price;
  final IconData icon;
  final String todoListId;

  const TodoItem({
    @required this.color,@required this.id,@required this.todoListId,@required this.price,@required this.title,@required this.icon,});}

我正在寻找 价格最高的TodoItem的标题

并将TodoItem添加到其中:

List<TodoItem> todo= [];

解决方法

void main(){
  

  List<ToDoItem> _myList = [];
  _myList.add(ToDoItem('10',100));
  
  _myList.add(ToDoItem('30',400));
  _myList.add(ToDoItem('40',2));
  
  _myList.add(ToDoItem('20',200));
  
  _myList.sort((a,b)=>a.price.compareTo(b.price));
  
  for(ToDoItem items in _myList)
    print(items.id + ',' + items.price.toString());
}

  class ToDoItem{
  final String id;
  final int price;

  const ToDoItem(
   this.id,this.price,);
  }

这是我在dart中用来对列表进行排序的代码。您可以从此处使用排序逻辑。它将按照价格升序对列表进行排序。