flutter sort list
someObjects.sort();
// By object property value
someObjects.sort((a, b) => a.someProperty.compareTo(b.someProperty));
// To reverse sort, just swab a and b
someObjects.sort((a, b) => b.someProperty.compareTo(a.someProperty));
flutter sort list
someObjects.sort();
// By object property value
someObjects.sort((a, b) => a.someProperty.compareTo(b.someProperty));
// To reverse sort, just swab a and b
someObjects.sort((a, b) => b.someProperty.compareTo(a.someProperty));
sort list flutter with a model
class _Person {
final int age;
final String name;
_Person({required this.age, required this.name});
}
void _test() {
final array = [
_Person(age: 10, name: 'Dean'),
_Person(age: 20, name: 'Jack'),
_Person(age: 30, name: 'Ben'),
];
// ascend with age
// Dean Jack Ben
array.sort((p1, p2) {
return Comparable.compare(p1.age, p2.age);
});
// decend with age
// Ben Jack Dean
array.sort((p1, p2) {
return Comparable.compare(p2.age, p1.age);
});
// ascend with name
// Ben Dean Jack
array.sort((p1, p2) {
return Comparable.compare(p1.name, p2.name);
});
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us