如何在 Dart 中对列表进行排序? [复制]
Posted
技术标签:
【中文标题】如何在 Dart 中对列表进行排序? [复制]【英文标题】:How to sort list in Dart? [duplicate] 【发布时间】:2020-02-10 21:06:18 【问题描述】:我想按价格从低到高排序列表
我想按价格从低到高排序列表 我想按价格从低到高排序列表 我想按价格从低到高排序列表 我想按价格从低到高排序列表
getProductsList()
List list = [];
list.add(
"id": "1",
"name": "sssss",
"imageUrl": "assets/images/Frozen Food.jpg",
"description": "qwerfghjhgfdsaasdfghnjmhgfdsa",
"price": "11",
"quantity": "1",
"isLiked": "false",
"max": "5"
);
list.add(
"id": "2",
"name": "sssss",
"imageUrl": "assets/images/Frozen Food.jpg",
"description": "qwerfghjhgfdsaasdfghnjmhgfdsa",
"price": "112",
"quantity": "1",
"isLiked": "true",
"max": "5"
);
list.add(
"id": "3",
"name": "sssss",
"imageUrl": "assets/images/Frozen Food.jpg",
"description": "qwerfghjhgfdsaasdfghnjmhgfdsa",
"price": "114",
"quantity": "1",
"isLiked": "true",
"max": "10"
);
list.add(
"id": "4",
"name": "sssssssssssssssssssssssssssssssssssssssssssssssssssssssszzzzz",
"imageUrl": "assets/images/Frozen Food.jpg",
"description": "qwerfghjhgfdsaasdfghnjmhgfdsa",
"price": "11",
"quantity": "1",
"isLiked": "false",
"max": "1"
);
list.add(
"id": "5",
"name": "sssss",
"imageUrl": "assets/images/Frozen Food.jpg",
"description": "qwerfghjhgfdsaasdfghnjmhgfdsa",
"price": "110",
"quantity": "1",
"isLiked": "false",
"max": "15"
);
return list;
【问题讨论】:
请提供一些您尝试过的代码,而不是想要排序的列表示例 【参考方案1】:你可以这样做:
list.sort((a, b) => a['price'].compareTo(b['price']));
【讨论】:
【参考方案2】:你可以试试这个-:
list.sort((ListModel a,ListModel b)=>a.getprice().compareTo(b.getprice()));
【讨论】:
以上是关于如何在 Dart 中对列表进行排序? [复制]的主要内容,如果未能解决你的问题,请参考以下文章