简体   繁体   中英

how to sort list by date - flutter/dart

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;
  }

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);
    });

you can use a higher order method called sort

and user compareTo to compare between dates ex:

   var items = [
    'Default',
    'Difficulty',
    'Time',
    'Di',
  ];
items.sort((a,b)=>a.length.compareTo(b.length));
print(items);
//[Di, Time, Default, Difficulty]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM