eMRe
eMRe

Reputation: 3247

Sorting the files by name returns unsorted array

I am having some problems on sorting the files by their name.

Number of records: 2048 comes before Number of records: 512

File Set 10.txt: comes after File Set 1.txt:

When i run my code I get this output:

Number of Records:  1024
File Set 1.txt:         NOT Sorted:     Sorted:     0.003441570093855262
File Set 10.txt:        NOT Sorted:     Sorted:     0.0017387439729645848
File Set 2.txt:         NOT Sorted:     Sorted:     7.515490287914872E-4
File Set 3.txt:         NOT Sorted:     Sorted:     0.0017166410107165575
File Set 4.txt:         NOT Sorted:     Sorted:     2.553839876782149E-4
File Set 5.txt:         NOT Sorted:     Sorted:     2.577530103735626E-4
File Set 6.txt:         NOT Sorted:     Sorted:     2.59725988144055E-4
File Set 7.txt:         NOT Sorted:     Sorted:     2.3920100647956133E-4
File Set 8.txt:         NOT Sorted:     Sorted:     2.344639942748472E-4
File Set 9.txt:         NOT Sorted:     Sorted:     2.6880399673245847E-4


Number of Records:  2048
File Set 1.txt:         NOT Sorted:     Sorted:     0.0010665359441190958
File Set 10.txt:        NOT Sorted:     Sorted:     0.0010333789978176355
File Set 2.txt:         NOT Sorted:     Sorted:     0.001001800992526114
File Set 3.txt:         NOT Sorted:     Sorted:     0.001030615996569395
File Set 4.txt:         NOT Sorted:     Sorted:     0.0010112749878317118
File Set 5.txt:         NOT Sorted:     Sorted:     9.946960490196943E-4
File Set 6.txt:         NOT Sorted:     Sorted:     0.0010148270521312952
File Set 7.txt:         NOT Sorted:     Sorted:     9.958799928426743E-4
File Set 8.txt:         NOT Sorted:     Sorted:     9.587769745849073E-4
File Set 9.txt:         NOT Sorted:     Sorted:     6.394480005837977E-4


Number of Records:  512
File Set 1.txt:         NOT Sorted:     Sorted:     3.828799890470691E-5
File Set 10.txt:        NOT Sorted:     Sorted:     3.868299972964451E-5
File Set 2.txt:         NOT Sorted:     Sorted:     3.868299972964451E-5
File Set 3.txt:         NOT Sorted:     Sorted:     3.828799890470691E-5
File Set 4.txt:         NOT Sorted:     Sorted:     3.789300171774812E-5
File Set 5.txt:         NOT Sorted:     Sorted:     3.828799890470691E-5
File Set 6.txt:         NOT Sorted:     Sorted:     3.710400051204488E-5
File Set 7.txt:         NOT Sorted:     Sorted:     6.433900125557557E-5
File Set 8.txt:         NOT Sorted:     Sorted:     6.236600165721029E-5
File Set 9.txt:         NOT Sorted:     Sorted:     6.473500252468511E-5

It suppose to be:

Number of Records:  512
File Set 1.txt:         NOT Sorted:     Sorted:     0.003441570093855262
File Set 2.txt:         NOT Sorted:     Sorted:     7.515490287914872E-4
File Set 3.txt:         NOT Sorted:     Sorted:     0.0017166410107165575
File Set 4.txt:         NOT Sorted:     Sorted:     2.553839876782149E-4
File Set 5.txt:         NOT Sorted:     Sorted:     2.577530103735626E-4
File Set 6.txt:         NOT Sorted:     Sorted:     2.59725988144055E-4
File Set 7.txt:         NOT Sorted:     Sorted:     2.3920100647956133E-4
File Set 8.txt:         NOT Sorted:     Sorted:     2.344639942748472E-4
File Set 9.txt:         NOT Sorted:     Sorted:     2.6880399673245847E-4
File Set 10.txt:        NOT Sorted:     Sorted:     0.0017387439729645848

Number of Records:  1024
File Set 1.txt:         NOT Sorted:     Sorted:     0.0010665359441190958
File Set 2.txt:         NOT Sorted:     Sorted:     0.001001800992526114
File Set 3.txt:         NOT Sorted:     Sorted:     0.001030615996569395
File Set 4.txt:         NOT Sorted:     Sorted:     0.0010112749878317118
File Set 5.txt:         NOT Sorted:     Sorted:     9.946960490196943E-4
File Set 6.txt:         NOT Sorted:     Sorted:     0.0010148270521312952
File Set 7.txt:         NOT Sorted:     Sorted:     9.958799928426743E-4
File Set 8.txt:         NOT Sorted:     Sorted:     9.587769745849073E-4
File Set 9.txt:         NOT Sorted:     Sorted:     6.394480005837977E-4
File Set 10.txt:        NOT Sorted:     Sorted:     0.0010333789978176355

Number of Records:  2048
File Set 1.txt:         NOT Sorted:     Sorted:     3.828799890470691E-5
File Set 2.txt:         NOT Sorted:     Sorted:     3.868299972964451E-5
File Set 3.txt:         NOT Sorted:     Sorted:     3.828799890470691E-5
File Set 4.txt:         NOT Sorted:     Sorted:     3.789300171774812E-5
File Set 5.txt:         NOT Sorted:     Sorted:     3.828799890470691E-5
File Set 6.txt:         NOT Sorted:     Sorted:     3.710400051204488E-5
File Set 7.txt:         NOT Sorted:     Sorted:     6.433900125557557E-5
File Set 8.txt:         NOT Sorted:     Sorted:     6.236600165721029E-5
File Set 9.txt:         NOT Sorted:     Sorted:     6.473500252468511E-5
File Set 10.txt:        NOT Sorted:     Sorted:     3.868299972964451E-5

This is my code:

boolean runTest(String[] text, int[] number, String url, String out, Sort sort) {
  PrintWriter filename;
  boolean tobeReturned = true;
  String beforeSorting = "";
  String afterSorting = "";

  long startTime;
  double timeTaken;
  try{
    filename = createWriter(out);
    File dir = new File(url);
    File[] listDir = dir.listFiles();
    Arrays.sort(listDir);

    for(File directory : listDir){

      File[] listOfFiles = directory.listFiles();
      Arrays.sort(listOfFiles);

      filename.println("Number of Records: \t" + directory.getName());
      for (File file : listOfFiles) {
        //println(file.getName());
        text = loadStrings(file);
        number = int(text);
        if(isSorted(number)){beforeSorting = "Sorted";}else{beforeSorting = "NOT Sorted";};

        startTime = startTime();       
        sort.sortInteger(number);        
        timeTaken = stopTime(startTime);

        if(isSorted(number)){afterSorting = "Sorted";}else{afterSorting = "NOT Sorted";};

        //filename.println("File Set " + file.getName() + ": \t\t" + stopTime(startTime()));
        filename.println("File Set " + file.getName() + ": \t\t" + beforeSorting + ": \t" + afterSorting + ": \t" + timeTaken);
        timeTaken = 0;
      }
      filename.println("\n");
    }
    filename.flush();
    filename.close();
  } catch (Exception e) {
    tobeReturned = false;
  }
  return tobeReturned;
}

Upvotes: 3

Views: 167

Answers (3)

Bon
Bon

Reputation: 3103

You need to implement your own Comparator that compare the filename based on the integer part.

Arrays.sort(listDir, new Comparator<File>() {
        @Override
        public int compare(File o1, File o2) {
            int num1 = Integer.parseInt(o1.getName().split("\\.")[0]);
            int num2 = Integer.parseInt(o2.getName().split("\\.")[0]);
            return num1 - num2;
        }
    });

Upvotes: 1

Florian L.
Florian L.

Reputation: 849

Have a look here: Java Natural Order comparator

And use it with:

Arrays.sort(listOfFiles, new NaturalOrderComparator());

Upvotes: 0

Nyavro
Nyavro

Reputation: 8866

Your files get sorted in lexicographical order. If you want to sort them in numeric order, you have to provide your custom Comparator where you should parse file name, extract numeric part and compare by this numeric part

Arrays.sort(listOfFiles, new Comparator<String>() {

        Integer getInt(String fileName) {
            //Implement number extraction
        }

        @Override
        public int compare(String fileNameA, String fileNameB) {
            return getInt(fileNameA).compareTo(getInt(fileNameA));
        }
    });

Upvotes: 3

Related Questions