简体   繁体   English

Java-如何按数字和字母顺序对2D数组排序?

[英]Java - How to sort a 2D array numerically and alphabetically?

I have a 2D array of Strings. 我有一个二维的字符串数组。 All the columns are String of numbers. 所有列都是数字String I need to sort the array in descending order by the columns and when the columns are the same, I need to sort them alphabetically by the words in the row. 我需要按列的降序对数组进行排序,当列相同时,我需要按行中的单词按字母顺序对它们进行排序。

Example: 例:

input = "Practice makes perfect. you'll only get Perfect by practice. just practice!"

output: [ ["practice", "3"], ["perfect", "2"],
      ["by", "1"], ["get", "1"], ["just", "1"],
      ["makes", "1"], ["only", "1"], ["youll", "1"]  ]

So I've done all the work, like parsing the input and counting the occurrence of words. 因此,我完成了所有工作,例如解析input并计算单词的出现。

So, I'm using a Comparator to sort them but it's not working. 因此,我正在使用比较器对它们进行排序,但无法正常工作。 My 2D array is called result and here is the code: 我的2D数组称为result ,下面是代码:

Arrays.sort(result, new Comparator<String[]>() {
    @Override
    public int compare(String[] a, String[] b) {
      int diff = b[1].compareTo(a[1]);
      if (diff == 0) {
        diff = b[0].compareTo(a[0]);
      }
      return diff;
    }
  }); 

They get sorted by occurrence just fine but then when the cols are the same there is no order to the words. 它们按出现的顺序排序就很好了,但是当cols相同时,单词没有顺序。

Thanks. 谢谢。

I think you want to solve below problem : Whatever input string, you want to make a 2d array where each unique words and their occurrence count store. 我认为您想解决以下问题:无论输入什么字符串,您都想创建一个二维数组,其中存储每个唯一单词及其出现次数。

Now you want to sort that 2D Array on the basis of word's occurrence count in descending order and if occurrence count matched among multiple word's occurrence count then sort them in descending order on the basis of alphabetically among them. 现在,您要根据单词的出现次数以降序对2D数组进行排序,如果出现次数在多个单词的出现次数之间匹配,则根据它们之间的字母顺序按降序对它们进行排序。

I am sorted Array without Comparator , It may be help you . 我在没有比较器的情况下对数组进行了排序,可能会对您有所帮助。

private static void sort2DArray(String[][] string2DArray) {
    int maxInt;
    String maxKey;
    String maxValue;
    int flag = 0;

    /* Sort the 2D Array */
    for (int m = 0; m < string2DArray.length; m++) {
        maxInt = Integer.parseInt(string2DArray[m][1]);
        for (int n = 0; n < string2DArray.length; n++) {

            /* swap on the basis of word's occurrences */
            if (maxInt > Integer.parseInt(string2DArray[n][1])) {
                maxKey = string2DArray[m][0];
                maxValue = string2DArray[m][1];
                string2DArray[m][0] = string2DArray[n][0];
                string2DArray[m][1] = string2DArray[n][1];
                string2DArray[n][0] = maxKey;
                string2DArray[n][1] = maxValue;
                maxInt = Integer.parseInt(string2DArray[n][1]);
            } else {

                /* swap on the basis of word */
                if (maxInt == Integer.parseInt(string2DArray[n][1])) {
                    flag = string2DArray[m][0].compareToIgnoreCase(string2DArray[n][0]);
                    if (flag > 0) {
                        maxKey = string2DArray[m][0];
                        maxValue = string2DArray[m][1];
                        string2DArray[m][0] = string2DArray[n][0];
                        string2DArray[m][1] = string2DArray[n][1];
                        string2DArray[n][0] = maxKey;
                        string2DArray[n][1] = maxValue;
                    }
                }
            }
        }
    }
}

For descending order of occurences and alphabetic order of words you can use a comparator of the below form 对于出现的降序和单词的字母顺序,您可以使用以下形式的比较器

Arrays.sort(arr,(a,b) -> a[1].compareTo(b[1])==0? a[0].compareTo(b[0]) : 0-(a[1].compareTo(b[1])));

I am using a lambda expression, which basically checks if the occurences are equal, and if so compares the values of the words, else it returns the comparison of the occurences with altered sign (to get them in descending order). 我使用的是lambda表达式,该表达式基本上检查出现次数是否相等,如果相等,则比较单词的值,否则返回带有更改符号的出现次数的比较(以降序排列)。

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

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