How to sort files using datetimestamp

后端 未结 3 1567
生来不讨喜
生来不讨喜 2020-12-20 09:16

I am capturing images, then storing into SD Card and showing in a List, but here i need a small change, still

3条回答
  •  一生所求
    2020-12-20 09:32

    If you getting data in reverse order than you can use reverse loop.

    Try below loop

    for (int i = files.length-1; i >= 0; i--)
    {
    file = files[i];
    Log.d("Count",file.getPath());
    it.add (file.getPath());
    }
    

    instead of

     for (int i = 0; i < files.length; i++)
     {
          file = files[i];
          Log.d("Count",file.getPath());
          it.add (file.getPath());
      }
    

    or sort data with particular field

    Sort array data before using in for loop and use same loop..

    Arrays.sort(files, new Comparator()
    {
        public int compare(Object o1, Object o2) {
    
            if (((File)o1).lastModified() > ((File)o2).lastModified()) {
                return -1;
            } else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
                return +1;
            } else {
                return 0;
            }
        }
    
    });
    
    for (int i = 0; i < files.length; i++)
     {
          file = files[i];
          Log.d("Count",file.getPath());
          it.add (file.getPath());
      }
    
        

    提交回复
    热议问题