Download StackFood Multi Restaurant, Food Delivery App with Laravel Admin and Restaurant Panel Buy Full App on $79
Download FlutKit - Flutter UI Kit Most beautiful Flutter UI Kit with 20+ Full App, 450+ Screens. Buy Full Kit on $39
Download Fluxstore Multi Vendor WooCommerce/Dokan Plugin compatible WordPress eCommerce Flutter App. Buy Full App on $79

How to sort List of object by property value in Dart/Flutter

In this example, we are going to show you how to sort an array list of object models by its property values like alphabets, numbers, dates on both ascending and descending order. See the example below:

Object Model Class:

class Student{
   String name, dob;
   int rollno;

   Student({ 
       required this.name,
       required this.rollno,
       required this.dob
   });
   
}

List of Object Model 'Student':

List<Student> students = [
    Student(name: "John Cena", rollno: 3, dob: "2003-10-23"),
    Student(name: "Krishna Chaudhary", rollno: 4, dob: "2005-01-13"),
    Student(name: "Jack Sparrow", rollno: 6, dob: "1993-03-15"),
    Student(name: "Harry Potter", rollno: 2, dob: "2011-09-05")
];

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

students.sort((a, b){ 
    return a.name.toLowerCase().compareTo(b.name.toLowerCase());
    //softing on alphabetical order (Ascending order by Name String)
});
//output order: Harry Potter, Jack Sparrow, John Cena, Krishna Chaudhary

students.sort((a, b){ 
    return b.name.toLowerCase().compareTo(a.name.toLowerCase());
    //softing on alphabetical order (Descending order by Name String)
});
//output order: Krishna Chaudhary, John Cena, Jack Sparrow, Harry Potter

Sort List of Object by Numberical Property in Dart/Flutter:

students.sort((a, b){ 
    return a.rollno.compareTo(b.rollno);
    //softing on numerical order (Ascending order by Roll No integer)
}); 
//output order: Harry Potter (2), John Cena (3), Krishna Chaudhary (4), Jack Sparrow(6)

students.sort((a, b){ 
    return b.rollno.compareTo(a.rollno);
    //softing on numerical order (Descending order by Roll No integer)
}); 
//output order: Jack Sparrow (6), Krishna Chaudhary (4), John Cena (3), Harry Potter (2)

Sort List of Object by Date Property in Dart/Flutter:

students.sort((a, b){ 
    return DateTime.parse(a.dob).compareTo(DateTime.parse(b.dob));
    //convert Date string to DateTime and compare
    //softing on DateTime order (Ascending order by DOB date string)
}); 
//output order: Jack Sparrow (1993-03-15), John Cena (2003-10-23), 
//              Krishna Chaudhary (2005-01-13), Harry Potter (2011-09-05)

students.sort((a, b){ 
    return DateTime.parse(b.dob).compareTo(DateTime.parse(a.dob));
    //convert Date string to DateTime and compare
    //softing on DateTime order (Descending order by DOB date string)
}); 
//output order: Harry Potter (2011-09-05), Krishna Chaudhary (2005-01-13),
//              John Cena (2003-10-23), Jack Sparrow (1993-03-15)

Full Flutter/Dart Code Example:

import 'package:flutter/material.dart';

void main() {
  runApp( MaterialApp(
       home: Home()
  ));
}

class Home extends  StatefulWidget {
  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {

  @override
  Widget build(BuildContext context) {

    List<Student> students = [
        Student(name: "John Cena", rollno: 3, dob: "2003-10-23"),
        Student(name: "Krishna Chaudhary", rollno: 4, dob: "2005-01-13"),
        Student(name: "Jack Sparrow", rollno: 6, dob: "1993-03-15"),
        Student(name: "Harry Potter", rollno: 2, dob: "2011-09-05")
    ];

    students.sort((a, b){ 
        return a.name.toLowerCase().compareTo(b.name.toLowerCase());
        //softing on alphabetical order (Ascending order by Name String)
    });

    return Scaffold(
         appBar: AppBar(
            title: Text("Sorting List of Object"),
            backgroundColor: Colors.redAccent,
         ),
          body: Container(
             alignment: Alignment.center,
             padding: EdgeInsets.all(20),
             child: Column(
               children:students.map((studentone){
                  return Container(
                    child: Card(
                       child:ListTile(
                          leading: Text(studentone.rollno.toString(), 
                                    style: TextStyle(fontSize: 25),),
                          title: Text(studentone.name),
                          subtitle: Text(studentone.dob),
                       )
                    )
                  );
               }).toList(),
             ),
          )
      );
  }
}

class Student{
   String name, dob;
   int rollno;

   Student({ 
       required this.name,
       required this.rollno,
       required this.dob
   });
   
}

Output Screenshot:

In this way, you can sort List of object models by its poverty value in Dart/Flutter.

No any Comments on this Article


Please Wait...