在Dart中对地图列表进行排序 - 在Dart中进行第二级排序

时间:2014-03-04 16:43:48

标签: sorting dart

我有这样的地图列表:

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。我可以使用单个地图或单个列表来管理它,但组合地图列表会使其更加困难。

感谢您的帮助。

3 个答案:

答案 0 :(得分:14)

要排序:

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'));

答案 1 :(得分:1)

import 'package:queries/collections.dart';

void main() {
  var a = new Collection<Map<String, String>>(associations);
  var m = new Collection<Map<String, String>>(members);
  // 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.
  var sortedMembers =
      m.orderBy((m) => m["firstName"]).thenBy((m) => m["lastName"]);

  printElements("Sorted members", sortedMembers);

  // 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.
  var someMembers = m.where((m) => m["lastName"].startsWith("D"));

  printElements("Some members", someMembers);

  // I am able to manage it with a single map or a single list,
  // but combining a list of maps makes it more difficult.
  var joinMembers = a.join(m, (a) => a["name"], (m) => m["associationName"],
      (a, m) => {"key": a["description"], "member": m});

  printElements("Join members", joinMembers);

  var groupMembers = a.groupJoin(
      m,
      (a) => a["name"],
      (m) => m["associationName"],
      (a, m) => {"key": a["description"], "members": m.toList()});

  printElements("Group members", groupMembers);
}

void printElements(String message, IEnumerable elements) {
  print("==================");
  print(message);
  for (var element in elements.asIterable()) {
    print(element);
  }
}

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'
  }
];

输出

==================
Sorted members
{associationName: EG, firstName: Bob, lastName: Dylan, email: bd@gmail.com}
{associationName: EG, firstName: Charles, lastName: Darwin, email: cd@gmail.com}
{associationName: NaVi, firstName: John, lastName: Malkovich, email: jm@gmail.com}
==================
Some members
{associationName: EG, firstName: Bob, lastName: Dylan, email: bd@gmail.com}
{associationName: EG, firstName: Charles, lastName: Darwin, email: cd@gmail.com}
==================
Join members
{key: Evil Genius, member: {associationName: EG, firstName: Bob, lastName: Dylan, email: bd@gmail.com}}
{key: Evil Genius, member: {associationName: EG, firstName: Charles, lastName: Darwin, email: cd@gmail.com}}
{key: Natus Vincere, member: {associationName: NaVi, firstName: John, lastName: Malkovich, email: jm@gmail.com}}
==================
Group members
{key: Evil Genius, members: [{associationName: EG, firstName: Bob, lastName: Dylan, email: bd@gmail.com}, {associationName: EG, firstName: Charles, lastName: Darwin, email: cd@gmail.com}]}
{key: Natus Vincere, members: [{associationName: NaVi, firstName: John, lastName: Malkovich, email: jm@gmail.com}]}

答案 2 :(得分:1)

List<Map> myList = [
  { 'name' : 'ifredom',age:23},
  { 'name' : 'JackMa',age:61},
  { 'name' : 'zhazhahui',age:48},
];

myList.sort((a, b) => (b.age).compareTo(a.age)); /// sort List<Map<String,dynamic>>

print(myList);