class Student {
String name;
String course;
int age;
Student(this.name, this.course, this.age);
@override
String toString() {
return '{$name, $course, $age}';
}
}
main() {
List<Student> students = [];
students.add(Student('Katherin', 'Dart Potions', 21));
students.add(Student('Adam Sr', 'Dart Magic', 40));
students.add(Student('Adam Jr', 'Dart Magic', 15));
students.sort(
(a, b) {
final int sortByCourse = -a.course.compareTo(b.course); // the minus '-' for descending
if (sortByCourse == 0) {
final int sortByName = a.name.compareTo(b.name);
if (sortByName == 0) {
return a.age.compareTo(b.age);
}
return sortByName;
}
return sortByCourse;
},
);
print('Sort DESC by Course, then ASC by Name and then ASC by Age:\n ${students.toString()}');
}
1条答案
按热度按时间xv8emn3q1#
下面是我如何在Dart中按三个或更多字段对列表进行排序: