{

How to Sort List of objects - int string date properties in ascending and descending in Dart or Flutter example


 Sort List of Objects sort in ascending and descending  in Dart or Flutter example

This tutorial shows multiple ways to sort a list or array of numbers or strings in ascending and descending order.

We can use either Array or List data structure used to sort numbers.

Consider the Employee Class which contains int, string, and DateTime properties as given.

Employee class contains fields - id, name, salary, and joinDate The toJson() method returns the JSON of an object.

class Employee {
  final int id;
  final String name;
  final int salary;
  final DateTime joinDate;

  Employee(this.id, this.name, this.salary, this.joinDate);

  Map<String, dynamic> toJson() =>
      {"id": id, "name": name, "salary": salary, "doj": joinDate.toString()};
}

Let’s add data to the employee object and create a list object. Print the object into JSOn format.

import 'dart:convert';

final e1 =
    Employee(11, 'Erwin', 9000, DateTime.parse("2020-06-21 00:00:00.000"));
final e2 =
    Employee(21, 'Andrew', 70000, DateTime.parse("2021-07-23 00:00:00.000"));
final e3 = Employee(4, 'Mark', 8000, DateTime.parse("2017-08-24 00:00:00.000"));
final e4 =
    Employee(12, 'Otroc', 5000, DateTime.parse("2022-12-05 00:00:00.000"));
void main() {
  final List<Employee> employees = [e1, e2, e3, e4];
  String json =
      jsonEncode(employees.map((i) => i.toJson()).toList()).toString();
  print(json);
}

formatted son Output:

[
  {
    "id": 11,
    "name": "Erwin",
    "salary": 9000,
    "doj": "2020-06-21 00:00:00.000"
  },
  {
    "id": 21,
    "name": "Andrew",
    "salary": 70000,
    "doj": "2021-07-23 00:00:00.000"
  },
  {
    "id": 4,
    "name": "Mark",
    "salary": 8000,
    "doj": "2017-08-24 00:00:00.000"
  },
  {
    "id": 12,
    "name": "Otroc",
    "salary": 5000,
    "doj": "2022-12-05 00:00:00.000"
  }
]

The list is displayed in the default list insertion order.

If you want to sort employee class objects based on a string, number, and date Let’s discuss different sorting techniques.

All the code here will work in Dart and Flutter.

Dart List Objects Sort in naturally or reverses the order

In this sort list class objects based on number properties in a Natural Order(Ascending order) and Reverse Order(Descending order)

To sort the list of types, use the sort() method as described below

Here is a syntax

sort(Optional CompareFunction)

CompareFunction is an optional Comparator function, without this, It results in sorted results in a natural order which is equal to the list.sort() method

For `ascending or natural order, you can override the sort methods as follows. Both results are the same results

  List.sort((obj1, obj2) => obj1.property.compareTo(obj2.property));

obj1.property.compareTo(obj2.property) function always return following

  • if obj1< obj2, returns < 0
  • if obj1= obj2, returns = 0
  • if obj1> obj2, returns > 0

For descending order, you can override the sort method or use the reversed property as follows.

  List.sort((obj1, obj2) => obj2.compareTo(obj1)); 

How to sort List objects based on number property type in natural and reverse order

In this, Sort the list of employees’ salaries in ascending and descending order.

Here is the sequence of steps for the natural order

  • Create a List of employee types with inline initialization syntax
  • sort again sorted the object number type in the natural order for the original list
  • Finally, print the list of the employee in JSON

And, Program:

  employees.sort((a, b) => a.salary.compareTo(b.salary));
  String json =
      jsonEncode(employees.map((i) => i.toJson()).toList()).toString();

  print(json);

Output:

[
  {
    "id": 12,
    "name": "Otroc",
    "salary": 5000,
    "doj": "2022-12-05 00:00:00.000"
  },
  {
    "id": 4,
    "name": "Mark",
    "salary": 8000,
    "doj": "2017-08-24 00:00:00.000"
  },
  {
    "id": 11,
    "name": "Erwin",
    "salary": 9000,
    "doj": "2020-06-21 00:00:00.000"
  },
  {
    "id": 21,
    "name": "Andrew",
    "salary": 70000,
    "doj": "2021-07-23 00:00:00.000"
  }
]

Here is the sequence of steps for reverse order

Sort list of objects properties in descending order

Program code:

  final List<Employee> employees = [e1, e2, e3, e4];

  employees.sort((a, b) => b.salary.compareTo(a.salary));

  String json =
      jsonEncode(employees.map((i) => i.toJson()).toList()).toString();

  print(json);
}

Output:

[
  {
    "id": 21,
    "name": "Andrew",
    "salary": 70000,
    "doj": "2021-07-23 00:00:00.000"
  },
  {
    "id": 11,
    "name": "Erwin",
    "salary": 9000,
    "doj": "2020-06-21 00:00:00.000"
  },
  {
    "id": 4,
    "name": "Mark",
    "salary": 8000,
    "doj": "2017-08-24 00:00:00.000"
  },
  {
    "id": 12,
    "name": "Otroc",
    "salary": 5000,
    "doj": "2022-12-05 00:00:00.000"
  }
]

How to sort List of Objects in property string in natural and reverse order

In this example, a Sort list of employees is based on string names in natural and reverse order.

Here is a list of employee names and used default sort() method sorts the strings in natural order by alphabet.

void main() {
  final List<Employee> employees = [e1, e2, e3, e4];

  employees.sort((obj1, obj2) => obj1.name.compareTo(obj2.name));

  String json =
      jsonEncode(employees.map((k) => k.toJson()).toList()).toString();

  print(json);
}

Output:

[
  {
    "id": 21,
    "name": "Andrew",
    "salary": 70000,
    "doj": "2021-07-23 00:00:00.000"
  },
  {
    "id": 11,
    "name": "Erwin",
    "salary": 9000,
    "doj": "2020-06-21 00:00:00.000"
  },
  {
    "id": 4,
    "name": "Mark",
    "salary": 8000,
    "doj": "2017-08-24 00:00:00.000"
  },
  {
    "id": 12,
    "name": "Otroc",
    "salary": 5000,
    "doj": "2022-12-05 00:00:00.000"
  }
]

Sort list of employee names in reverse order i.e reverse order

void main() {
  final List<Employee> employees = [e1, e2, e3, e4];

  employees.sort((obj1, obj2) => obj2.name.compareTo(obj1.name));

  String json =
      jsonEncode(employees.map((k) => k.toJson()).toList()).toString();

  print(json);
}

Output:

[
  {
    "id": 12,
    "name": "Otroc",
    "salary": 5000,
    "doj": "2022-12-05 00:00:00.000"
  },
  {
    "id": 4,
    "name": "Mark",
    "salary": 8000,
    "doj": "2017-08-24 00:00:00.000"
  },
  {
    "id": 11,
    "name": "Erwin",
    "salary": 9000,
    "doj": "2020-06-21 00:00:00.000"
  },
  {
    "id": 21,
    "name": "Andrew",
    "salary": 70000,
    "doj": "2021-07-23 00:00:00.000"
  }
]

How to sort the list of objects on the DateTime property in ascending and descending order?

In this example, Employee objects are sorted based on the join date property in ascending and descending order.

Example program to sort the list of objects with DateTime property in ascending order.

void main() {
  final List<Employee> employees = [e1, e2, e3, e4];

  employees.sort((obj1, obj2) => obj1.joinDate.compareTo(obj2.joinDate));

  String json =
      jsonEncode(employees.map((k) => k.toJson()).toList()).toString();

  print(json);
}

Output:

[
  {
    "id": 4,
    "name": "Mark",
    "salary": 8000,
    "doj": "2017-08-24 00:00:00.000"
  },
  {
    "id": 11,
    "name": "Erwin",
    "salary": 9000,
    "doj": "2020-06-21 00:00:00.000"
  },
  {
    "id": 21,
    "name": "Andrew",
    "salary": 70000,
    "doj": "2021-07-23 00:00:00.000"
  },
  {
    "id": 12,
    "name": "Otroc",
    "salary": 5000,
    "doj": "2022-12-05 00:00:00.000"
  }
]

Example program to sort the list of objects with DateTime property in descending order.

void main() {
  final List<Employee> employees = [e1, e2, e3, e4];

  employees.sort((obj1, obj2) => obj2.joinDate.compareTo(obj1.joinDate));

  String json =
      jsonEncode(employees.map((k) => k.toJson()).toList()).toString();

  print(json);
}

Output:

[
  {
    "id": 12,
    "name": "Otroc",
    "salary": 5000,
    "doj": "2022-12-05 00:00:00.000"
  },
  {
    "id": 21,
    "name": "Andrew",
    "salary": 70000,
    "doj": "2021-07-23 00:00:00.000"
  },
  {
    "id": 11,
    "name": "Erwin",
    "salary": 9000,
    "doj": "2020-06-21 00:00:00.000"
  },
  {
    "id": 4,
    "name": "Mark",
    "salary": 8000,
    "doj": "2017-08-24 00:00:00.000"
  }
]

Conclusion

To summarize, Learn how to sort the list of class objects in dart and flutter programming

  • Object String property
  • Object number property
  • Object DateTime property
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.