How to Sort List of numbers or String in ascending and descending in Dart or Flutter example
This tutorial demonstrates several methods for sorting a list or array of numbers or strings in both ascending and descending order. You can utilize either the Array
or List
data structure to accomplish this sorting task.
How to sort a List of Numbers in natural and reverse order
Let’s consider we have a set of numbers in a list:
var numbers = [33, 3, 31, 4, 40];
This is a list of numbers in Dart and Flutter, which can be sorted in two ways:
- Natural Order (Ascending order)
- Reverse Order (Descending order)
To sort the list, we use the sort()
method.
Below is the syntax
sort(Optional CompareFunction)
The CompareFunction is an optional Comparator function. Without it, the list is sorted in natural order, equivalent to using the list.sort() method.
For ascending or natural order, you can use either of the following methods, which yield the same results:
List.sort((num1, num2) => num1.compareTo(num2));
List.sort()
The num1.compareTo(num2) function returns:
- < 0 if num1 < num2
- 0 if num1 = num2
0 if num1 > num2
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 are the steps for sorting in natural order.
- Create a
List
of integers with inline initialization. Sort
the numbers in natural order using the sort method.- Finally, print the sorted list of numbers.
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]
}
And here are the steps for sorting in reverse order:
- Create a
List
of integers with inline initialization. Sort
the numbers in reverse order using the sort method with a custom comparator.- Finally, print the sorted 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 a List of Strings in Natural and Reverse Order
Lists of strings can be sorted in ascending or descending order.
The List.sort()
method sorts strings in natural order, i.e., ascending order.
Here’s an example of sorting a list of strings in natural order using the default sort()
method, which sorts strings alphabetically.
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]
}
To sort a list of strings in 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]
}
In this example, the sort()
method is supplied with a custom comparator function (str1, str2) => str2.compareTo(str1)
to sort the strings in descending order.