user2904796
user2904796

Reputation: 267

Java Embedded Comparator Class

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

Upvotes: 0

Views: 437

Answers (3)

Stanley Stein
Stanley Stein

Reputation: 397

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

Upvotes: 0

Jyoti Ranjan Pattnaik
Jyoti Ranjan Pattnaik

Reputation: 713

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/

Upvotes: 0

Sanket Meghani
Sanket Meghani

Reputation: 905

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;
}

Upvotes: 2

Related Questions