Dart| Flutter:对数字或字符串的列表或数组进行升序和降序排序的多种方法

1,479 阅读6分钟

本教程展示了对数字或字符串的列表或数组进行升序和降序排序的多种方法。

我们可以使用数组或列表的数据结构来对数字进行排序。

考虑到Employee类,它包含int、string、DateTime属性,如图所示。

Employee 类包含字段 id- name , salary, joinDatetoJson()方法返回一个对象的JSON。

class Employee {
  final int id;
  final String name;
  final int salary;
  final DateTime joinDate;

  Employee(this.id, this.name, this.salary, this.joinDate);

  Map toJson() =>
      {"id": id, "name": name, "salary": salary, "doj": joinDate.toString()};
}

让我们向雇员对象添加数据,创建一个列表对象。将该对象打印成JSOn格式。

import 'dart:convert';

final e1 =
    Employee(11, 'Erwin', 9000, DateTime.parse("2020-06-21 00:00:00.000"));
final e2 =
    Employee(21, 'Andrew', 70000, DateTime.parse("2021-07-23 00:00:00.000"));
final e3 = Employee(4, 'Mark', 8000, DateTime.parse("2017-08-24 00:00:00.000"));
final e4 =
    Employee(12, 'Otroc', 5000, DateTime.parse("2022-12-05 00:00:00.000"));
void main() {
  final List employees = [e1, e2, e3, e4];
  String json =
      jsonEncode(employees.map((i) => i.toJson()).toList()).toString();
  print(json);
}

输出:

[  {    "id": 11,    "name": "Erwin",    "salary": 9000,    "doj": "2020-06-21 00:00:00.000"  },  {    "id": 21,    "name": "Andrew",    "salary": 70000,    "doj": "2021-07-23 00:00:00.000"  },  {    "id": 4,    "name": "Mark",    "salary": 8000,    "doj": "2017-08-24 00:00:00.000"  },  {    "id": 12,    "name": "Otroc",    "salary": 5000,    "doj": "2022-12-05 00:00:00.000"  }]

列表是以默认的列表插入顺序显示的。

如果你想根据字符串、数字和日期对雇员类对象进行排序 让我们讨论一下不同的排序技术。

这里的所有代码都可以在DartFlutter

Dart 列表对象以自然或颠倒的顺序排序

在这里,基于数字属性的列表类对象以自然顺序(升序)和反转顺序(降序)进行排序。

要对列表类型进行排序,请使用排序()方法,如下所述

下面是一个语法

sort(Optional CompareFunction)

CompareFunction是一个可选的比较器函数,如果没有这个函数,它的结果是以自然顺序排序,相当于list.sort()方法。

对于`升序或自然顺序,你可以覆盖排序方法,如下所示。两者的结果都是一样的

  List.sort((obj1, obj2) => obj1.property.compareTo(obj2.property));

obj1.property.compareTo(obj2.property)函数总是返回以下结果

  • 如果obj1< obj2,返回< 0
  • 如果obj1= obj2,返回值=0
  • 如果obj1>obj2,返回值>0

对于descending order ,你可以覆盖排序方法或使用reversed 属性,如下所示。

  List.sort((obj1, obj2) => obj2.compareTo(obj1)); 

如何根据数字属性类型对列表对象进行自然和反向排序

在此,以升序和降序对雇员工资列表进行排序。

以下是自然排序的步骤顺序

  • 用内联初始化语法创建一个List 的雇员类型
  • sort 再次按自然顺序对原始列表中的对象数字类型进行排序
  • 最后,在JSON中打印雇员的列表

还有,程序:

  employees.sort((a, b) => a.salary.compareTo(b.salary));

  String json =
      jsonEncode(employees.map((i) => i.toJson()).toList()).toString();

  print(json);

输出:

[  {    "id": 12,    "name": "Otroc",    "salary": 5000,    "doj": "2022-12-05 00:00:00.000"  },  {    "id": 4,    "name": "Mark",    "salary": 8000,    "doj": "2017-08-24 00:00:00.000"  },  {    "id": 11,    "name": "Erwin",    "salary": 9000,    "doj": "2020-06-21 00:00:00.000"  },  {    "id": 21,    "name": "Andrew",    "salary": 70000,    "doj": "2021-07-23 00:00:00.000"  }]

下面是反向排序的步骤顺序

按降序对对象属性列表进行排序

程序代码:

  final List employees = [e1, e2, e3, e4];

  employees.sort((a, b) => b.salary.compareTo(a.salary));

  String json =
      jsonEncode(employees.map((i) => i.toJson()).toList()).toString();

  print(json);
}

输出:

[  {    "id": 21,    "name": "Andrew",    "salary": 70000,    "doj": "2021-07-23 00:00:00.000"  },  {    "id": 11,    "name": "Erwin",    "salary": 9000,    "doj": "2020-06-21 00:00:00.000"  },  {    "id": 4,    "name": "Mark",    "salary": 8000,    "doj": "2017-08-24 00:00:00.000"  },  {    "id": 12,    "name": "Otroc",    "salary": 5000,    "doj": "2022-12-05 00:00:00.000"  }]

如何以自然和逆向顺序对属性字符串中的对象列表进行排序

在这个例子中,雇员的排序列表是基于自然和反向顺序的字符串名称。

这里有一个雇员名字的列表,使用默认的sort()方法按字母的自然顺序对字符串进行排序。

void main() {
  final List employees = [e1, e2, e3, e4];

  employees.sort((obj1, obj2) => obj1.name.compareTo(obj2.name));

  String json =
      jsonEncode(employees.map((k) => k.toJson()).toList()).toString();

  print(json);
}

输出:

[  {    "id": 21,    "name": "Andrew",    "salary": 70000,    "doj": "2021-07-23 00:00:00.000"  },  {    "id": 11,    "name": "Erwin",    "salary": 9000,    "doj": "2020-06-21 00:00:00.000"  },  {    "id": 4,    "name": "Mark",    "salary": 8000,    "doj": "2017-08-24 00:00:00.000"  },  {    "id": 12,    "name": "Otroc",    "salary": 5000,    "doj": "2022-12-05 00:00:00.000"  }]

以相反的顺序对雇员姓名进行排序,即反向排序。

void main() {
  final List employees = [e1, e2, e3, e4];

  employees.sort((obj1, obj2) => obj2.name.compareTo(obj1.name));

  String json =
      jsonEncode(employees.map((k) => k.toJson()).toList()).toString();

  print(json);
}

输出:

[  {    "id": 12,    "name": "Otroc",    "salary": 5000,    "doj": "2022-12-05 00:00:00.000"  },  {    "id": 4,    "name": "Mark",    "salary": 8000,    "doj": "2017-08-24 00:00:00.000"  },  {    "id": 11,    "name": "Erwin",    "salary": 9000,    "doj": "2020-06-21 00:00:00.000"  },  {    "id": 21,    "name": "Andrew",    "salary": 70000,    "doj": "2021-07-23 00:00:00.000"  }]

如何以升序和降序对DateTime属性的对象列表进行排序?

在这个例子中,雇员对象是根据joinDate属性以升序和降序排序的。

用Datetime属性对对象列表进行升序排序的程序示例。

void main() {
  final List employees = [e1, e2, e3, e4];

  employees.sort((obj1, obj2) => obj1.joinDate.compareTo(obj2.joinDate));

  String json =
      jsonEncode(employees.map((k) => k.toJson()).toList()).toString();

  print(json);
}

输出:

[  {    "id": 4,    "name": "Mark",    "salary": 8000,    "doj": "2017-08-24 00:00:00.000"  },  {    "id": 11,    "name": "Erwin",    "salary": 9000,    "doj": "2020-06-21 00:00:00.000"  },  {    "id": 21,    "name": "Andrew",    "salary": 70000,    "doj": "2021-07-23 00:00:00.000"  },  {    "id": 12,    "name": "Otroc",    "salary": 5000,    "doj": "2022-12-05 00:00:00.000"  }]

用DateTime属性对对象列表进行降序排序的示例程序。

void main() {
  final List employees = [e1, e2, e3, e4];

  employees.sort((obj1, obj2) => obj2.joinDate.compareTo(obj1.joinDate));

  String json =
      jsonEncode(employees.map((k) => k.toJson()).toList()).toString();

  print(json);
}

输出:

[  {    "id": 12,    "name": "Otroc",    "salary": 5000,    "doj": "2022-12-05 00:00:00.000"  },  {    "id": 21,    "name": "Andrew",    "salary": 70000,    "doj": "2021-07-23 00:00:00.000"  },  {    "id": 11,    "name": "Erwin",    "salary": 9000,    "doj": "2020-06-21 00:00:00.000"  },  {    "id": 4,    "name": "Mark",    "salary": 8000,    "doj": "2017-08-24 00:00:00.000"  }]

总结

综上所述,学习如何在dart和flutter编程中对类对象的列表进行排序

  • 对象字符串属性
  • 对象数字属性
  • 对象日期时间属性