Hello friends, In today’s tutorial we would learn about 2 Type of Array List Sorting methods, Ascending Sorting and Descending Sorting. In Flutter DART if we want to sort our Number list in these format then we have to use the ListName.sort() method. This method allow us to sort the list into ascending order automatically. But again to sort list in descending order we have to use ListName.reversed function at the time of printing elements. For in easy language we must have to use ListName.sort() method in both cases, But if we do not use the ListName.reversed function at the time of printing elements then our list will print in Ascending order and if we use the ListName.reversed function at the time of printing then it will print in Descending order. So in this tutorial we would learn about Sort Number List in Ascending Descending Order Flutter Dart.
Example :-
1. Ascending Sorting :-
1 2 3 |
numbers.sort(); print('Ascending order: $numbers'); |
2. Descending Sorting :-
1 2 3 |
numbers.sort(); print('Descending order ${numbers.reversed}'); |
Now after seeing the above example, You can easily understand How it all works here.
Contents in this project Sort Number List in Ascending Descending Order Flutter Dart :-
Complete source code for main.dart file :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Flutter Sort List in Ascending Order')), body: App())); } } class App extends StatefulWidget { AppState createState() => AppState(); } class AppState extends State { @override void initState() { // Sorting List in Ascending Order. numbers.sort(); super.initState(); } final List<double> numbers = <double>[ 1, 1.3, 8, 7, 8.3, 7.7, 5.3, 2, 99, 100, 12, 15, 101 ]; @override Widget build(BuildContext context) { return ListView.builder( itemCount: numbers.length, itemBuilder: (BuildContext context, int index) { return Container( height: 50, margin: const EdgeInsets.all(2), color: Color.fromARGB(255, 2, 39, 252), child: Center( child: Text( '${numbers[index]}', style: const TextStyle(fontSize: 24, color: Colors.white), )), ); }); } } |
Screenshot :-