The core libraries in Dart are responsible for the existence of the List class, its creation, and manipulation

Sorting of the list depends on the type of list we are sorting ie if we are sorting an integer list then we can use a simple sort function whereas if it is a string list then we use compareTo to sort the list. Sorts the list according to the order specified by the compare function.

So In this article, we are going to sort the list of data into different factors which are given below.

  • Sorting a list of Number
  • Sorting a list of Strings
  • Sorting a list of Maps
  • Sorting a list of Objects

Sorting a list of Number

Using the sort() method helps us to quickly sort a list of numbers in ascending or descending order. Note that this method does not return a new list but manipulates the original list.


void main() {
  List<int> myNumbers = [11, 23, 37, 43, -30, 85, 77];
  myNumbers.sort();
  print('Ascending order is given by: $myNumbers');
  print('Descending order is given by ${myNumbers.reversed}');
}

Output:

Ascending order is given by: [-30, 11, 23, 37, 43, 77, 85]
Descending order is given by (85, 77, 43, 37, 23, 11, -30)

Sorting a list of Strings

This example is similar to the first one. We use the sort() method to sort the string elements of a list in alphabetical order (from A to Z or from Z to A).

void main() {
  List<String> name = [
    'sagar',
    'koju',
    'sanam',
    'keshav',
    'pancha',
    'mobile',
    'flutter',
    'deno',
    'dart',
  ];
 name.sort();
  print('Ascending order (A to Z) is given by: $name');
  print('Descending order (Z to A) is given by : ${name.reversed}');
}

Output:

Ascending order (A to Z) is given by: [dart, deno, flutter, keshav, koju, mobile, pancha, sagar, sanam]
Descending order (Z to A) is given by : (sanam, sagar, pancha, mobile, koju, keshav, flutter, deno, 
dart)
[dart, deno, flutter, keshav, koju, mobile, pancha, sagar, sanam]

Sorting a list of Maps

Sorting a list of maps is a little bit more complex. We need to use the sort() method and the compareTo() method. which look like in the below code:

void main() {
  List<Map<String, dynamic>> products = [
    {"name": "laptop", "total": 150000},
    {"name": "keyboard", "total": 500},
    {"name": "Book", "total": 400},
    {"name": "chair", "total": 5000},
    {"name": "head phone", "total": 900}
  ];

  // Selling price from low to high
  products.sort(
    (a, b) => a['total'].compareTo(b['total']),
  );
  print('Low to hight in price is given by: $products');

  // Selling price from high to low
  products.sort((a, b) => b['total'].compareTo(a['total']));
  print('High to low in price is given by: $products');
}

Output:

Low to hight in price is given by: [{name: Book, total: 400}, {name: keyboard, total: 500}, {name: head phone, total: 900}, {name: chair, total: 5000}, {name: laptop, total: 150000}]
High to low in price is given by: [{name: laptop, total: 150000}, {name: chair, total: 5000}, {name: head phone, total: 900}, {name: keyboard, total: 500}, {name: Book, total: 400}]

Sorting a list of Objects

Similar to sorting a list of maps, we can sort a list of objects by the property values with the sort() and compareTo() methods which look like in the below code:

class User {
  final String name;
  final int age;

  User({required this.name, required this.age});

  @override
  toString() {
    return ('$name - $age years old \n');
  }
}

void main() {
  List<User> users = [
    User(name: 'Sagar', age: 24),
    User(name: 'Sanam', age: 29),
    User(name: 'Keshab', age: 49),
    User(name: 'Pancha Laxmi', age: 48),
    User(name: 'Manju', age: 28),
  ];

  // Age from small to large
  users.sort((a, b) => a.age.compareTo(b.age));
  print('Age ascending order is given by: ${users.toString()}');

  // Age from large to small
  users.sort((a, b) => b.age.compareTo(a.age));
  print('Age descending order given by: ${users.toString()}');
}

Here we are going to sort the age according to the ascending and descending order which has the following output:

Age ascending order is given by: [Sagar - 24 years old 
, Manju - 28 years old
, Sanam - 29 years old
, Pancha Laxmi - 48 years old
, Keshab - 49 years old
]
Age descending order given by: [Keshab - 49 years old
, Pancha Laxmi - 48 years old
, Sanam - 29 years old
, Manju - 28 years old
, Sagar - 24 years old
]

Thank you for reading the Guide about How to Sort the List of Data in Dart.