Sorting a List of Objects in Dart by Property Value

Sorting a List of Objects in Dart by Property Value

19 December 2024 Stephan Petzl Leave a comment Tech-Help

Sorting a list of objects by a specific property is a common requirement in software development. This article guides you through the process of sorting objects in Dart, specifically within the Flutter framework. We’ll explore practical examples to help you implement sorting in your projects effectively.

Understanding List Sorting in Dart

Dart provides a straightforward way to sort lists using the List.sort method. You can pass a custom comparison function to this method to define the sorting logic. Here’s a basic example:


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

In this example, someObjects is the list, and someProperty is the property by which you want to sort the objects. The compareTo method is used to compare the values of someProperty.

Advanced Sorting Techniques

For more complex sorting requirements, such as sorting by multiple properties or implementing a stable sort, you can use more advanced techniques. Below are examples of how to achieve these:

Sorting by Multiple Properties

If you need to sort a list by multiple properties, you can chain comparison functions. For instance, to sort by surname and then by given name, you can implement a comparison function that checks both properties:


class Name {
  Name({String? surname, String? givenName})
    : surname = surname ?? "",
      givenName = givenName ?? "";

  final String surname;
  final String givenName;
}

int compareNames(Name name1, Name name2) {
  var comparisonResult = name1.surname.compareTo(name2.surname);
  if (comparisonResult != 0) {
     return comparisonResult;
  }
  return name1.givenName.compareTo(name2.givenName);
}
    

Reversing the Sort Order

If you need to reverse the sort order, you can either modify the comparison function to return the opposite sign or reverse the sorted list:


list = (list..sort()).reversed.toList();
    

Stable Sort Implementations

For a stable sort, consider using the package:collection which provides insertionSort and mergeSort functions.

Practical Example

Let’s look at how to implement a custom sorting function for a class that implements the Comparable interface:


class MyCustomClass implements Comparable {
  final String someProperty;

  MyCustomClass(this.someProperty);

  @override
  int compareTo(MyCustomClass other) {
    return someProperty.compareTo(other.someProperty);
  }
}

void main() {
  List list = [/* some instances */];
  list.sort();
}
    

Enhancing Testing with Repeato

If you’re developing mobile applications with Flutter, ensuring that your sorting logic functions correctly across different scenarios is crucial. This is where Repeato, a no-code test automation tool for iOS and Android, can be highly beneficial. Repeato allows you to create, run, and maintain automated tests for your apps efficiently. By leveraging computer vision and AI, it simplifies the testing process, making it fast to edit and execute tests. To learn more about how Repeato can streamline your testing workflow, visit our Flutter test automation page.

Like this article? there’s more where that came from!