0

Use I am using sketchware IDE. How can I sort the the list by date or time modified.

I used this code

Arrays.sort(list, new Comparator<File>(){ 
 @Override public int compare(File file1, File file2)
  { 
      long k = file1.lastModified() - file2.lastModified();
       if(k > 0)
       { return 1; }
       else 
       if(k == 0)
       { return 0; }
       else
       { return -1; 
           } 
           }
            }
            );

But I get an error:

The method sort(T[], Comparator<? super T>) in the type Arrays is not applicable for the arguments (ArrayList<String>, new Comparator<File>(){})
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

0

Arrays.sort() doesn't seem to accept a List.

So if you want to use Arrays.sort(), you should first convert the list to an array, like this:

File[] filesArray = new File[files.size()];
filesArray = files.toArray(filesArray);

Then call the function with the filesArray variable.

Most Noble Rabbit
  • 2,728
  • 2
  • 6
  • 12