Want to sort/order a list by date in dart/flutter? Use the below example to sort/order a list by date in dart/flutter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import 'package:intl/intl.dart'; void main() { List products = [ "2019-11-25 00:00:00.000", "2019-11-22 00:00:00.000", "2019-11-22 00:00:00.000", "2019-11-24 00:00:00.000", "2019-11-23 00:00:00.000" ]; List<DateTime> newProducts = []; DateFormat format = DateFormat("yyyy-MM-dd"); for (int i = 0; i < 5; i++) { newProducts.add(format.parse(products[i])); } newProducts.sort((a,b) => a.compareTo(b)); for (int i = 0; i < 5; i++) { print(newProducts[i]); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.