2

I'm trying to sort filenames that's in fList array. Originally fList is sorted like this https://i.sstatic.net/OCqJR.jpg and after result https://i.sstatic.net/GgBsq.jpg

I wanted the result sorted according to their preceding number before the filename like so:

    1. Selenium Introduction
    1. Complete Installation Guide for java and Selenium Learning
    1. Brush up Java Concepts for Selenium Automation
    1. CORE JAVA In depth for Manual testers and Beginners

...

    1. Bonus!! Student Special

I have something() method to get the number from fList[x] filename to later compare when doing the swap(fList[x], fList[y]) as you can tell from output console. I'm not sure I understand how File[] actually stores and changes its elements

public static void main(String[] args) {

        File file = new File("pathToFolder");
        File[] fList = file.listFiles();
        for(int x = 0; x < fList.length; x++) {
            int numberX = something(fList[x]);
            for(int y = x; y < fList.length; y++) {
                int numberY = something(fList[y]);
                if(numberX > numberY) {
                    File temp = fList[x];
                    fList[x] = fList[y];
                    fList[y] = temp;
                }
            }
        }
        for(int x = 0; x < fList.length; x++) {         
            System.out.println(fList[x].getName());
        }

    }

    static int something(File file) {
        String temp = file.getName();
        String number = "";
        for(int st = 0; st < temp.length(); st++) {
            if(temp.charAt(st) == '.') break;
            number += temp.charAt(st);
        }
        int fileNumber = Integer.parseInt(number); 
        return fileNumber;
    }

4
  • 2
    You're caching numberX but not updating it when you swap. Commented Dec 5, 2019 at 22:36
  • No, it's an easy enough mistake to make. Commented Dec 5, 2019 at 22:41
  • 1
    Why not just call the [Arrays.sort overload](docs.oracle.com/javase/7/docs/api/java/util/… that takes a Comparator and convert you something method into a Comparator.compare method? Commented Dec 5, 2019 at 23:01
  • Your code can be also written as Arrays.sort( fList, Comparator.comparing( File::getName, Comparator.comparingInt( name -> Integer.parseInt( name.split("\\.")[0] ) ) ) ) OR using your something method Arrays.sort( fList, Comparator.comparingInt(YourClassName::something) ); Commented Dec 5, 2019 at 23:06

2 Answers 2

1

Try this if you're using Java8, you can of course tweak it to match your requirements:

List<File> sortedFiles = stream(requireNonNull(file.listFiles()))
            .sorted(File::compareTo)
            .collect(toList());

You can use also the sort method provided by the class Arrays:

File[] sortedFiles = file.listFiles();
    // One liner
    Arrays.sort(sortedFiles);

Both of the solution rely on the implementation of the compareTo from the File class. Link to the official documentation.

I added a test in my personal git repo

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

Comments

0

How about below code:

public static void main(String[] args) {
    File file = new File("pathToFolder");
    File[] fList = file.listFiles();
    Map<Integer, File> fileMap = new TreeMap<Integer, File>();
    for (int x = 0; x < fList.length; x++) {
        int numberX = something(fList[x]);
        fileMap.put(numberX, fList[x]);
    }

    fileMap.forEach((k, v) -> System.out.println((v.getName())));

}

static int something(File file) {
    String temp = file.getName();
    String number = temp.split("\\.")[0];
    int fileNumber = Integer.parseInt(number);
    return fileNumber;
}

1 Comment

Your solution may work for OPs use case. But I wouldnt add a map just to sort files. Besides if there are two files with the same number, e.g. 1. CORE JAVA and 1. CORE C++ using your approach will loose one of the files as you cann't have duplicate keys in map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.