[SOLVED] how to sort list by date – flutter/dart

Issue

This Content is from Stack Overflow. Question asked by shakky

I’m using storage access framework to get media files from specific directory. I’m making the use of saf.cache() method which returns the list of string (basically media path)

but I need to sort this list of string by creation time/date. How can I do that?

https://pub.dev/packages/saf

  Future<List<String>> fetchImages() async {

      Saf saf = Saf('specific_directory');
      List<String>? cacheList = await saf.cache();
      List<String> imageList = cacheList!.where((element) => element.endsWith('.jpg')).toList(); // it returns In random order
      return imageList;
  }



Solution

Use .sort property to do this.

try:

myList.sort((sl1, sl2) {
      String list1 = sl1['column1'] ?? '0';
      String list2 = sl2['column2'] ?? '0';
      return sl1.compareTo(sl2);
    });


This Question was asked in StackOverflow by shakky and Answered by Sergio Clemente It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?