Flutter Dart:按双精度对地图列表进行排序
我有一个dart中的地图列表,我想对其进行排序 我使用的列表:Flutter Dart:按双精度对地图列表进行排序,flutter,dart,Flutter,Dart,我有一个dart中的地图列表,我想对其进行排序 我使用的列表: List<myModel> myList; (我想对视图进行排序,从高到低) 但我得到了这个错误: error: A value of type 'SplayTreeMap' can't be assigned to a variable of type 'List<myModel>'. 这很有效 有可能这样排序吗?您不能将列表传递给SplayTreeMap.from(…) 见: SplayTreeMa
List<myModel> myList;
(我想对视图进行排序,从高到低)
但我得到了这个错误:
error: A value of type 'SplayTreeMap' can't be assigned to a variable of type 'List<myModel>'.
这很有效
有可能这样排序吗?您不能将
列表
传递给SplayTreeMap.from(…)
见:
SplayTreeMap.from(映射其他,[int-compare(K键1 K键2),bool-isValidKey(动态电位键)])
您需要使用映射
或使用SplayTreeMap.fromIterable(…)
():
SplayTreeMap.fromIterable(Iterable Iterable,{K键(动态元素)、V值(动态元素)、int比较(K键1 K键2)、bool isValidKey(动态电位键)})
您可以通过以下方式实现:
final filteredList=
SplayTreeMap.fromIterable(
myList,
键:(m)=>m,
值:(m)=>m.views,
比较:(a,b)=>
int.parse(a.views)。比较(int.parse(b.views))
);
以上是一个例子,请根据您的需要进行调整
编辑2:根据您的编辑,您可以执行以下操作:
final temp=List.from(myList);
临时排序((a,b)=>int.parse(a.views.compareTo(int.parse(b.views));
过滤器列表=温度;
非常感谢!在我的案例中如何实现它?@karledbedts我添加了一个示例。@karledbedts过滤器列表的类型是什么?是List
?@karledbedts您不能将SplayTreeMap
分配给列表
。filteredList
是一个列表,您试图将SplayTreeMap
分配给它,这两种类型彼此不兼容。@karledbedts欢迎您=)。很高兴我能帮忙!
error: A value of type 'SplayTreeMap' can't be assigned to a variable of type 'List<myModel>'.
filteredList = myList
.where((snapshot) => snapshot.views > 0)
.toList();