Flutter - How to Sort a list of objects in Flutter (Dart) by properties value - 2023

In the flutter app development, We need to deal with an array list for storing the same type of data. Sometimes we need to sort data according to its properties so I am going to share with you "How to Sort a list of objects in Flutter (Dart) by properties value" values like numbers, dates, and alphabets on both ascending and descending order. Let's see the following example.

Model:

First, we take a model which has basic details of employees like below.

class Employee {
  int empId;
  String name, dob;

  Employee({required this.empId, required this.name, required this.dob});
}

 

Employee List:

Then we make a list of employees like below

  List<Employee> employeeList = [
    Employee(empId:1,name: "Chris Evans", dob: "2008-12-12"),
    Employee(empId:2,name: "Robert Downey",  dob: "2002-03-28"),
    Employee(empId:3,name: "Jennifer Lawrence", dob: "1995-06-30"),
    Employee(empId:4,name: "Channing Tatum",  dob: "2014-11-02")];

 

Sort List of Objects by Alphabetical Properties in Dart/Flutter:

 You can see the below example we sort list by name in Ascending order.

 employeeList.sort((a, b){
      return a.name.toLowerCase().compareTo(b.name.toLowerCase());
    });

 

Output:

You can see the output in the below screenshot.

 

Sort List of Objects by Numerical Properties in Dart/Flutter:

You can see the below example we sort list by empId in Descending order.

employeeList.sort((a, b){
      return b.empId.compareTo(a.empId);
    });

 

Output:

You can see the output in the below screenshot.

 

Sort List of Objects by Date properties in Dart/Flutter:

In this example, we are going to sort the list using the date properties of the model, So first we need to convert the String date to DateTime.

1). We sort the list in Ascending order by Date.

employeeList.sort((a, b){
      return DateTime.parse(a.dob).compareTo(DateTime.parse(b.dob));
    });

 

Output:

You can see the output in the below screenshot.

 

2). We sort the list in Descending order by Date.

employeeList.sort((a, b){
      return DateTime.parse(b.dob).compareTo(DateTime.parse(a.dob));
    });

 

Output:

You can see the output in the below screenshot.

 

Full Example:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Errorgram',
      home: const Errorgram(title: 'Errorgram'),
    );
  }
}

class Errorgram extends StatefulWidget {
  const Errorgram({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<Errorgram> createState() => _ErrorgramState();
}

class _ErrorgramState extends State<Errorgram> {
  List<Employee> employeeList = [
    Employee(empId: 1, name: "Chris Evans", dob: "2008-12-12"),
    Employee(empId: 2, name: "Robert Downey", dob: "2002-03-28"),
    Employee(empId: 3, name: "Jennifer Lawrence", dob: "1995-06-30"),
    Employee(empId: 4, name: "Channing Tatum", dob: "2014-11-02")
  ];

  @override
  void initState() {
    employeeList.sort((a, b) {
      return DateTime.parse(b.dob).compareTo(DateTime.parse(a.dob));
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.red,
          title: Text("Errorgram"),
        ),
        body: Container(
          alignment: Alignment.center,
          padding: EdgeInsets.all(20),
          child: Column(
            children: employeeList.map((it) {
              return Container(
                  child: Card(
                      child: ListTile(
                leading: Text(
                  it.empId.toString(),
                  style: TextStyle(fontSize: 30),
                ),
                title: Text(it.name,style: TextStyle(color: Colors.amber,fontSize: 22),),
                subtitle: Text(it.dob,style: TextStyle(color: Colors.orange))),
              ));
            }).toList(),
          ),
        ));
  }
}

class Employee {
  int empId;
  String name, dob;

  Employee({required this.empId, required this.name, required this.dob});
}

 

 

Conclusion:

In flutter, we can easily Sort a list of objects in Flutter (Dart) by properties value. I hope this article may help you.

Tags: