如何对具有自定义元素(而不是字符串或整数)的列表进行排序?

问题描述 投票:0回答:1

我正在使用flutter_secure_storage软件包,并且正在尝试对列表中的项目进行排序。列表如下:List<_SecItems> hi = []

并且定义:

class _SecItem {
  _SecItem(this.key, this.value);

  final String key;
  final String value;
}

未保留列表中项目的顺序。并使用hi.sort()给出消息type '_SecItem' is not a subtype of type 'Comparable<dynamic>'

那么我将如何排序_Secitem元素列表中的字符串? List <_secitems>内部的元素仍然是字符串。

android sorting flutter dart comparable
1个回答
0
投票

您可以像这样对对象进行排序

hi.sort((a, b) => b.value.compareTo(a.value));
© www.soinside.com 2019 - 2024. All rights reserved.