Sort a list of objects in Flutter (Dart) by proper

2020-01-27 07:49发布

问题:

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

回答1:

You can pass a comparison function to List.sort.

someObjects.sort((a, b) => a.someProperty.compareTo(b.someProperty));


回答2:

If you want to sort the object "objects" by the property "name" do something like this

objects.sort((a, b) {
  return a.value['name'].toString().toLowerCase().compareTo(b.value['name'].toString().toLowerCase());
});    


标签: dart flutter