How to Sort List by Date in Dart/Flutter

In this example, we are going to show you how to sort the Array List of DateTime and Date string in both ascending and descending order in Dart or Flutter. See the example below for more details.

List<String> datestrings = [
  "2021-12-11", 
  "2020-02-12", 
  "2010-01-23",
  "2013-01-14",
];

datestrings.sort((a, b){ //sorting in ascending order
    return DateTime.parse(a).compareTo(DateTime.parse(b));
});

print(datestrings);
//output: [2010-01-23, 2013-01-14, 2020-02-12, 2021-12-11]

List<String> datestrings = [
  "2021-12-11", 
  "2020-02-12", 
  "2010-01-23",
  "2013-01-14",
];

datestrings.sort((a, b){ //sorting in descending order
    return DateTime.parse(b).compareTo(DateTime.parse(a));
});
print(datestrings);
//output: [2021-12-11, 2020-02-12, 2013-01-14, 2010-01-23]

To sort List on Descending order in Dart, just swap the place of "a" and "b".

List<DateTime> dates = [
  DateTime.parse("2021-12-11"), 
  DateTime.parse("2020-02-12"), 
  DateTime.parse("2010-01-23"),
];

dates.sort((a, b){ //sorting in ascending order
    return a.compareTo(b);
});
print(dates);
//output: [2010-01-23 00:00:00.000, 2020-02-12 00:00:00.000, 2021-12-11 00:00:00.000]

List<DateTime> dates = [
  DateTime.parse("2021-12-11"), 
  DateTime.parse("2020-02-12"), 
  DateTime.parse("2010-01-23"),
];

dates.sort((a, b){ //sorting in descending order
    return b.compareTo(a);
});
print(dates);
//output: [2021-12-11 00:00:00.000, 2020-02-12 00:00:00.000, 2010-01-23 00:00:00.000]

Swap the position of "a" and "b" to sort List in descending order.

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<String> datestrings = [
      "2021-12-11", 
      "2020-02-12", 
      "2010-01-23",
      "2013-01-14",
    ];

    datestrings.sort((a, b){ //sorting in ascending order
        return DateTime.parse(a).compareTo(DateTime.parse(b));
    });

    return Scaffold(
         appBar: AppBar(
            title: Text("Sorting Dates in Flutter"),
            backgroundColor: Colors.redAccent,
         ),
          body: Container(
             alignment: Alignment.center,
             padding: EdgeInsets.all(20),
             child: Column(
               children:datestrings.map((dateone){
                  return Container(
                    child: Card(
                       child:Container( 
                         width: double.infinity,
                         padding: EdgeInsets.all(15),
                         child: Text(dateone, style: TextStyle(fontSize: 18))),
                       ),
                  );
               }).toList(),
             ),
          )
      );
  }
}

In this way, you can soft Array List of Date String or DateTime in Dart/Flutter App.

No any Comments on this Article


Please Wait...