简体   繁体   English

Java嵌入式比较器类

[英]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/"} 传递给程序的数组如下所示: {"/","/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? 我的Arrays.sort()调用是否使用我的modifiend比较方法?

Thanks a lot, Junaid 非常感谢,Junaid

By default Arrays.sort() method uses natural order for sorting. 默认情况下,Arrays.sort()方法使用自然顺序进行排序。 Since the array in your case is of String, by default it will sort based on alphabetical order of String. 由于您的案例中的数组是String,因此默认情况下它将根据String的字母顺序进行排序。

To get the result you want, you will have to pass an instance of custom comparator implementation to Arrays.sort(). 要获得所需的结果,您必须将自定义比较器实现的实例传递给Arrays.sort()。

Replace your public String[] sort(String[] dirs) method in APTComp class with this: 用以下代码替换APTComp类中的public String[] sort(String[] dirs)方法:

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

Arrays.sort does not sort by your comparator directly except you call. 除了你调用之外,Arrays.sort不会直接按比较器排序。 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/ 结果:0:/ 1:/ games / 2:/ homework / 3:/ usr / 4:/ games / snake / 5:/ temp / downloads / 6:/ usr / local / 7:/ usr / local / bin /

See another example 另一个例子

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: OUTPUT:
/ /
/games/ /游戏/
/homework/ /家庭作业/
/usr/ 在/ usr /
/games/snake/ /游戏/蛇/
/temp/downloads/ /温度/下载/
/usr/local/ 在/ usr /本地/
/usr/local/bin/ 在/ usr / local / bin目录/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM