0

I am having trouble compiling some code that I wrote. The code is meant to sort an array of directories, and then return the sorted array. The arrays being passed into the program look like this: {"/","/usr/","/usr/local/","/usr/local/bin/","/games/","/games/snake/","/homework/","/temp/downloads/"}

The sorted answer for this would be:

{ "/", "/games/", "/homework/", "/usr/", "/games/snake/", 
     "/temp/downloads/", "/usr/local/", "/usr/local/bin/" }

So basically, the directories which are the least deep are placed first. If two directories have the same depth, we sort alphabetically depending on the first word. My code so far is this:

import java.util.Arrays;
import java.util.Comparator;

public class Dirsort {

  class APTComp implements Comparator<String> {

      public int compare(String a, String b) {
          String[] d1 = a.split("/");
          String[] d2 = b.split("/");
          int diff = d1.length - d2.length;

          if (diff != 0) {
            return diff;
          } //{"/","/usr/","/usr/local/","/usr/local/bin/","/games/","/games/snake/","/homework/","/temp/downloads/"}

          return a.compareTo(b);
        }



      public String[] sort(String[] dirs) {
        Arrays.sort(dirs);
        return dirs;
      }
}

Can you guys tell me anything you find wrong here? Does my Arrays.sort() call use my modifiend compare method?

Thanks a lot, Junaid

Pranav Singh
  • 17,079
  • 30
  • 77
  • 104
user2904796
  • 267
  • 1
  • 6
  • 19

3 Answers3

2

By default Arrays.sort() method uses natural order for sorting. Since the array in your case is of String, by default it will sort based on alphabetical order of String.

To get the result you want, you will have to pass an instance of custom comparator implementation to Arrays.sort().

Replace your public String[] sort(String[] dirs) method in APTComp class with this:

public String[] sort(String[] dirs) 
{
    Arrays.sort(dirs, new APTComp());
    return dirs;
}
Sanket Meghani
  • 885
  • 2
  • 14
  • 22
  • Hi. So I ran it again and currently, the code is simply sorting by alphabetical order and not the depth of the directory. The depth of the directory should be the first criterion being used to sort the array. Is there something wrong with my comparator method? – user2904796 Feb 11 '14 at 06:53
  • Your comparator method implementation looks good. Above method gives me correct result. The resultant array it gives is / /games/ /homework/ /usr/ /games/snake/ /temp/downloads/ /usr/local/ /usr/local/bin/ – Sanket Meghani Feb 11 '14 at 07:09
0

Arrays.sort does not sort by your comparator directly except you call. You should use

Arrays.sort(dirs, new APTComp());

The revised code:

import java.util.Arrays;
import java.util.Comparator;

public class Main {

  class APTComp implements Comparator<String> {

      public int compare(String a, String b) {
          String[] d1 = a.split("/");
          String[] d2 = b.split("/");
          int diff = d1.length - d2.length;

          if (diff != 0) {
            return diff;
          } //{"/","/usr/","/usr/local/","/usr/local/bin/","/games/","/games/snake/","/homework/","/temp/downloads/"}

          return a.compareTo(b);
        }
  }


      public String[] sort(String[] dirs) {
        Arrays.sort(dirs, new APTComp());
        return dirs;
      }

      public static void main(String[] args) {
          Main main = new Main();
          String[] result = main.sort(new String[] {"/","/usr/","/usr/local/","/usr/local/bin/","/games/","/games/snake/","/homework/","/temp/downloads/"});
          for(int i=0; i<result.length; i++) {
            System.out.println(i + ": " + result[i]);
          }
      }
}

Result: 0: / 1: /games/ 2: /homework/ 3: /usr/ 4: /games/snake/ 5: /temp/downloads/ 6: /usr/local/ 7: /usr/local/bin/

See another example

Community
  • 1
  • 1
Stanley Stein
  • 397
  • 1
  • 3
  • 17
  • Hi. I made the change you suggested. However it still says there are parsing errors in the code. – user2904796 Feb 11 '14 at 05:43
  • Hi. So I ran it again and currently, the code is simply sorting by alphabetical order and not the depth of the directory. The depth of the directory should be the first criterion being used to sort the array. Is there something wrong with my comparator method? – user2904796 Feb 11 '14 at 06:24
  • the revised code and the output have been provided. – Stanley Stein Feb 11 '14 at 08:33
0

This code is working, Please check

import java.util.Arrays;
import java.util.Comparator;

public class Dirsort {

class APTComp implements Comparator<String> {

    public int compare(String a, String b) {
        String[] d1 = a.split("/");
        String[] d2 = b.split("/");
        int diff = d1.length - d2.length;
        if (diff != 0) {
            return diff;
        }
        return a.compareTo(b);
    }

    public String[] sort(String[] dirs) {
        Arrays.sort(dirs);
        return dirs;
    }
}

public static void main (String[] args) {
  String[] arr = new String[] {"/","/usr/","/usr/local/","/usr/local/bin/","/games/","/games/snake/","/homework/","/temp/downloads/"};
Dirsort ds = new Dirsort();
  Arrays.sort(arr, ds.new APTComp());
  for (String s : arr) {
      System.out.println(s);
  }
}
}

OUTPUT:
/
/games/
/homework/
/usr/
/games/snake/
/temp/downloads/
/usr/local/
/usr/local/bin/

Jyoti Ranjan
  • 703
  • 2
  • 13
  • 29
  • Hello. Yeah the code is working but it is not sorting the way it should. The directories should be sorted by depth first, and then if there are two directories of the same depth, they should be sorted alphabetically. – user2904796 Feb 11 '14 at 06:30
  • Oh, sorry, I just misunderstood, now I edited the code. Please check – Jyoti Ranjan Feb 11 '14 at 08:19