-1

I have a folder with a lot of txt, images etc. How can I sort it by size and by files type?

File dirlist = new File(suuid);         
String[] extensions = new String[]    { "txt" };
List<File> files = (List<File>) FileUtils.listFiles(dirlist, extensions, true);         
for (File file : files) {
    // how can    I do ?

}

Thanks !

mataka
  • 115
  • 1
  • 10
  • @Carcigenicate yes is a better word – mataka Aug 25 '18 at 12:24
  • @Pshemo Classifiate by type and size if you want. – mataka Aug 25 '18 at 12:25
  • 1
    Your code formatting could be improved as it's very hard to read your code. Also, what have you tried so far? I have edited your code for readability, but in the future, you'll want to do this yourself. – DontKnowMuchBut Getting Better Aug 25 '18 at 12:25
  • @DontKnowMuchButGettingBetter yes I know, I try to put all the size in a array but it not very pratical – mataka Aug 25 '18 at 12:26
  • Could you provide some example of input and expected result? Like should it be ordered in ascending or descending order, or do you first want to group them by type and then order that group internally? – Pshemo Aug 25 '18 at 12:28
  • Possible duplicate of [How can I sort files in a directory in java?](https://stackoverflow.com/questions/8107001/how-can-i-sort-files-in-a-directory-in-java) – DontKnowMuchBut Getting Better Aug 25 '18 at 12:28

1 Answers1

0

A file's length() represents it's size, you can just sort by it:

files.sort(
    Comparator.comparingLong(File::length)
              .thenComparing(f -> f.getName().substring(f.getName().lastIndexOf(".") + 1)));
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • I'm going to try that – mataka Aug 25 '18 at 12:41
  • @Mureinik I seriously suspect this is not what the OP wants. Probably a `Map>` where key is the extension of the file and List is the file is some predefined order... – Eugene Aug 25 '18 at 12:43
  • Well, the question was "How can I sort it by size and by files type?" If the requirement was to create a map it's not obvious from the question, at least to me. – Mureinik Aug 25 '18 at 12:48