DEV Community

Cover image for Dart sort List by two fields
Tien Nguyen
Tien Nguyen

Posted on

7 2

Dart sort List by two fields

In this tutorial, we'll show you some ways and functions to sort list by two fields in Dart/Flutter

For sorting multiple fields, or two fields for this tutorial. I will also show you two ways:

  • using custom compare function.
  • extending Comparable abstract class and override compareTo() method

Full post: Dart/Flutter Sort List of objects

Dart Sort list by two fields using custom compare function

We're gonna pass a more complicated custom compare function into list's sort() method.
First we sort the list of objects by field name, then field age.

class Customer {
  String name;
  int age;

  Customer(this.name, this.age);

  @override
  String toString() {
    return '{ ${this.name}, ${this.age} }';
  }
}

main() {
  List<Customer> customers = [];
  customers.add(Customer('Jack', 23));
  customers.add(Customer('Adam', 27));
  customers.add(Customer('Katherin', 25));

  customers.sort((a, b) {
    int nameComp = a.name.compareTo(b.name);
    if (nameComp == 0) {
      return -a.age.compareTo(b.age); // '-' for descending
    }
    return nameComp;
  });
  print('Sort by Name(ASC), then Age(DESC):\n' + customers.toString());
}
Enter fullscreen mode Exit fullscreen mode

Output:

Sort by Name(ASC), then Age(DESC):
[{ Adam, 27 }, { Jack, 32 }, { Jack, 23 }, { Katherin, 25 }]

Dart Sort list by two fields using Comparable abstract class

The second approach is to extend Comparable abstract class and override compareTo() method. Now we don't need to pass compare function, we just call list.sort() instead of list.sort(compare).

class Customer extends Comparable {
  String name;
  int age;

  Customer(this.name, this.age);

  @override
  String toString() {
    return '{ ${this.name}, ${this.age} }';
  }

  // sort by Name (asc)
  @override
  int compareTo(other) {
    return this.name.compareTo(other.name);
  }
}

main() {
 List<Customer> customers = [];
  customers.add(Customer('Jack', 23));
  customers.add(Customer('Adam', 27));
  customers.add(Customer('Katherin', 25));
  customers.add(Customer('Jack', 32));

  customers.sort();
  print('Sort by Name(ASC), then Age(DESC):\n' + customers.toString());
}
Enter fullscreen mode Exit fullscreen mode

Output:

Sort by Name(ASC), then Age(DESC):
[{ Adam, 27 }, { Jack, 32 }, { Jack, 23 }, { Katherin, 25 }]

Further Reading

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️