简体   繁体   English

Flutter; 小部件的排序列表

[英]Flutter; Sort List of Widgets

I currently have a list of widgets我目前有一个小部件列表

List<LessonItem> _lessons = [];

One Widget inside this list, added by the user should look like this此列表中的一个小部件,由用户添加,应如下所示

LessonItem('Bio', 'Mrs Pithan', 'A1.012', HourMinute('08', '00'),
        HourMinute('11', '50'), Colors.green), //lesson, teacher, room, from, to, color

Now I want to sort the list by a specific property from the widgets.现在我想按小部件中的特定属性对列表进行排序。 How can I do that?我怎样才能做到这一点?

Thanks for any help!谢谢你的帮助!

I would try with the sort method in dart.我会尝试使用 dart 中的排序方法。

You choose a property you want to sort by, and then define how any two elements will be compared.您选择要排序的属性,然后定义如何比较任意两个元素。

Here I'm comparing each element by id (smaller id would en up first after sort):在这里,我按 id 比较每个元素(较小的 id 将在排序后首先出现):

  _lessons.sort((LessonItem a, LessonItem b) => a.id - b.id);

In this case I'm sorting by name (with the compareTo() method from String):在这种情况下,我按名称排序(使用 String 中的 compareTo() 方法):

  _lessons.sort((LessonItem a, LessonItem b) => a.name.compareTo(b.name));

You can find more detailed info and some examples in: dart documentation and this helpful post您可以在以下位置找到更详细的信息和一些示例: dart 文档这篇有用的帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM