{

How to Sort List of numbers or String 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.  Sort List of numbers  and strings in Dart or Flutter example We can use either Array or List data structured used to sort numbers.

How to sort 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 list.sort() method

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

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

num1.compareTo(num2) function always return following

  • if num1< num2, returns < 0
  • if num1= num2, returns = 0
  • if num1> num2, returns > 0
  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 alphabets.

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.





Related posts

Dart| Flutter How to: Find a Given String is numeric or not

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example