Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort a list of objects in Flutter (Dart) by property value

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)?

like image 756
Nomnom Avatar asked Nov 29 '18 21:11

Nomnom


People also ask

How do you sort list of objects in flutter?

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.

How do you sort a string list in darts?

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.

How do you sort items in ListView in flutter?

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.


1 Answers

You can pass a comparison function to List.sort.

someObjects.sort((a, b) => a.someProperty.compareTo(b.someProperty)); 
like image 111
Nate Bosch Avatar answered Oct 16 '22 12:10

Nate Bosch