0

This program simply sorts a folder of images files by size and outputs them to a new directory. However whenever I view the directory it is not maintained in the sorted order. Any solutions would be beneficial.

Here are the relevant code snippets

class CompareFileSize implements Comparator<File>{ public int compare(File s1, File s2) { long len1 = s1.length(); long len2 = s2.length(); if (len1 > len2){ return 1; }else if (len1 < len2){ return -1; }else return 0; } } public class FileSorter { public static void main(String[] args) throws IOException { File p = new File("C:\\Users\\PC\\Desktop\\newFolder"); File f = new File("C:\\Users\\PC\\Desktop\\oldFolder"); File myPics [] = f.listFiles(); ArrayList<File> aList = new ArrayList<File>(); for (int i = 0; i < myPics.length; i++){ aList.add(myPics[i]); } System.out.println("Before----------------------------------------"); for (File uimage: aList){ System.out.println(uimage); } System.out.println("After-----------------------------------------"); Collections.sort(aList, new CompareFileSize()); for (File image: aList){ System.out.println(image); FileUtils.copyFileToDirectory(image, p); } } } 
1
  • 1
    How do you "view the directory"? Commented May 20, 2015 at 3:22

1 Answer 1

3

However whenever I view the directory it is not maintained in the sorted order. Any solutions would be beneficial.

Why there may be an inherent order to how files are stored in a directory, your directory viewer will always override this with the current preference (sort by filesize, sort by modified date, sort by name alphabetically).

Your code won't have any effect.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I didn't know you couldn't store them in the order specified by the code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.