{

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example


 Sort List of numbers  and strings 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.

How to sort a List of Numbers in natural and reverse order

Let us say We have a set of numbers in a list.

  var numbers = [33, 3, 31, 4, 40];

This sort of List of numbers in dart and flutter

  • Natural Order(Ascending order)
  • 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

  List.sort((num1, num2) => num1.compareTo(num2));
  List.sort()

num1.compareTo(num2) function always returns the following

  • if num1< num2, returns < 0
  • if num1= num2, returns = 0
  • if num1> num2, returns > 0

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

  List.sort((num1, num2) => num2.compareTo(num1)); or
  List.reversed

Here is the sequence of steps for the natural order

  • Create a List of int types with inline initialization syntax
  • sort again sorted the numbers in the natural order for the original list
  • Finally, print the list of numbers

And, Program:

void main() {
  final List<int> numbers = <int>[33, 3, 31, 4, 40];
  print(numbers); //[33, 3, 31, 4, 40]

  numbers.sort();
  print(numbers); // [3, 4, 31, 33, 40]

}

Here is the sequence of steps for reverse order

  • Create a List of numeric values with inline initialization syntax
  • sort supplied with function and sorted the numbers in the reverse order
  • Finally, print the list of numbers

Program code:

void main() {
  final List<int> numbers = <int>[33, 3, 31, 4, 40];
  print(numbers); //[33, 3, 31, 4, 40]

  numbers.sort((num1, num2) => num2.compareTo(num1));
  print(numbers); // [40, 33, 31, 4, 3]
}

How to sort List of Strings in natural and reverse order

List of strings can be sorted in ascending order or descending.

List.sort() method sorts method natural order i.e ascending order

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

void main() {
  final List<String> words = <String>['one', 'two', 'three', 'four'];
  print(words); //[one, two, three, four]
  words.sort();
  print(words); // [four, one, three, two]
}

Sort list of strings in reverse order i.e reverse order

void main() {
  final List<String> words = <String>['one', 'two', 'three', 'four'];
  print(words); //[one, two, three, four]
  words.sort((str1, str2) => str2.compareTo(str1));
  print(words); // [two, three, one, four]

}
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.