How to sort List of object by property value in Dart/Flutter
Introduction
In Dart and Flutter, sorting a list of objects by a specific property value is a common requirement in many applications. This can be achieved using various methods, including the use of the `sort` function and the `compareTo` method. In this article, we will explore the different ways to sort a list of objects in Dart and Flutter.
Sorting Using the Sort Function
The `sort` function in Dart is a built-in function that can be used to sort a list of objects. To sort a list of objects by a specific property value, you can use the `sort` function and provide a callback function that compares two objects based on the property value.
“`dart
class Person {
String name;
int age;
Person({required this.name, required this.age});
}
void main() {
List
Person(name: ‘John’, age: 25),
Person(name: ‘Alice’, age: 30),
Person(name: ‘Bob’, age: 20),
];
people.sort((a, b) => a.age.compareTo(b.age));
print(people);
}
“`
Sorting Using the Comparable Interface
Another way to sort a list of objects in Dart is to implement the `Comparable` interface. This interface requires the implementation of the `compareTo` method, which compares two objects based on a specific property value.
“`dart
class Person implements Comparable
String name;
int age;
Person({required this.name, required this.age});
@override
int compareTo(Person other) {
return age.compareTo(other.age);
}
}
void main() {
List
Person(name: ‘John’, age: 25),
Person(name: ‘Alice’, age: 30),
Person(name: ‘Bob’, age: 20),
];
people.sort();
print(people);
}
“`
Sorting Using the ThenBy Function
The `thenBy` function is used to sort a list of objects by multiple property values. This function is useful when you need to sort a list of objects by more than one property value.
“`dart
class Person {
String name;
int age;
Person({required this.name, required this.age});
}
void main() {
List
Person(name: ‘John’, age: 25),
Person(name: ‘Alice’, age: 30),
Person(name: ‘Bob’, age: 20),
Person(name: ‘Charlie’, age: 25),
];
people.sort((a, b) {
int ageCompare = a.age.compareTo(b.age);
if (ageCompare == 0) {
return a.name.compareTo(b.name);
} else {
return ageCompare;
}
});
print(people);
}
“`
Conclusion
In conclusion, sorting a list of objects by a specific property value in Dart and Flutter can be achieved using various methods, including the use of the `sort` function, the `compareTo` method, and the `thenBy` function. By understanding these methods, you can efficiently sort lists of objects in your Dart and Flutter applications.
Frequently Asked Questions
1. How do I sort a list of objects in Dart?
You can sort a list of objects in Dart using the `sort` function and providing a callback function that compares two objects based on the property value.
2. Can I sort a list of objects by multiple property values in Dart?
Yes, you can sort a list of objects by multiple property values in Dart using the `thenBy` function.
3. What is the `Comparable` interface in Dart?
The `Comparable` interface in Dart requires the implementation of the `compareTo` method, which compares two objects based on a specific property value.
4. How do I implement the `Comparable` interface in a Dart class?
You can implement the `Comparable` interface in a Dart class by overriding the `compareTo` method and comparing two objects based on a specific property value.
5. Can I sort a list of objects in a Flutter application?
Yes, you can sort a list of objects in a Flutter application using the same methods as in a Dart application.