How to sort a list of objects by the alphabetical order of one of its properties (Not the name but the actual value the property holds)?
Pass a custom compare function into list's sort() method with swap the places of the items. We can sort a List of objects in Descending order using reversed property. It returns an Iterable of the objects in the list. Firstly, we extend Comparable abstract class and override compareTo() method, then we call list.
The core libraries in Dart are responsible for the existence of List class, its creation, and manipulation. Sorting of the list depends on the type of list we are sorting i.e. if we are sorting integer list then we can use simple sort function whereas if it is a string list then we use compareTo to sort the list.
You should sort the data before creating the ListView . You can use basic dart sort method. Basically you just need to implement the compare function which returns an integer that the sign of the return value specify items order.
You can pass a comparison function to List.sort
.
someObjects.sort((a, b) => a.someProperty.compareTo(b.someProperty));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With