在Dart-Second Level中对地图列表进行排序 [英] Sort a list of maps in Dart - Second Level Sort in Dart

查看:95
本文介绍了在Dart-Second Level中对地图列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的地图列表:

I have a list of maps like this:

var associations = [{'name': 'EG', 'description': 'Evil Genius'},
                    {'name': 'NaVi', 'description': 'Natus Vincere'}];

var members = [
{'associationName': 'EG', 'firstName': 'Bob', 'lastName': 'Dylan', 'email': 'bd@gmail.com'},
{'associationName': 'NaVi', 'firstName': 'John', 'lastName': 'Malkovich', 'email': 'jm@gmail.com'},
{'associationName': 'EG', 'firstName': 'Charles', 'lastName': 'Darwin', 'email': 'cd@gmail.com'}
];

我想编写一个代码,该代码将按姓氏,然后按名字的字母顺序对成员列表进行排序.此外,我希望能够找到其姓氏以指定字母开头的成员.例如,使用D,我们将得到Bob Dylan和Charles Darwin.我可以使用一个地图或一个列表来管理它,但是将地图列表组合起来会更加困难.

I would like to write a code that would sort the list of members alphabetically by the last name first, then by the first name. Moreover, I would like to be able to find members whose lastnames start with a specifiedd letter. By example, with D, we would get Bob Dylan and Charles Darwin. I am able to manage it with a single map or a single list, but combining a list of maps makes it more difficult.

感谢您的帮助.

推荐答案

要排序:

members.sort((m1, m2) {
  var r = m1["lastName"].compareTo(m2["lastName"]);
  if (r != 0) return r;
  return m1["firstName"].compareTo(m2["firstName"]);
});

要过滤:

members.where((m) => m['lastName'].startsWith('D'));

这篇关于在Dart-Second Level中对地图列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆