首页 > 解决方案 > 如何对具有自定义元素(不是字符串或 int)的列表进行排序?

问题描述

我正在使用 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> 中的元素仍然是字符串。

标签: androidsortingflutterdartcomparable

解决方案


实现 Comparable 并在 compareTo() 中编写逻辑。

class _SecItem implements Comaparable<_SecItem> {
_SecItem(this.key, this.value);
final String key;
final String value;
   public int compareTo(Student st){  


   }
}

推荐阅读