English 中文(简体)
需要帮助将两个层面阵列按第二要素分类,然后按第一要素分类(Java)
原标题:Need help sorting two dimensional arrays by second element and then by first element (Java)

我有两个方面,拥有5张卡。 5个阵列中每个阵列的第一个成分是卡片,第二个要素是卡片价值。

我想按第二个要素对2个阵列进行分类,然后按第一个要素分类,同时保持第二个要素的分类顺序(如果是这样的话)。 举例来说,所有诉讼案在分类清单中都将低于两套诉讼案。 例如,{0,1},{2,1},{0,2>{0,1},{2,1},{0,2>

在这方面,我已经:

 // {{3,2}, {2,2}, {0,1}, {1,0}, {2,3}} should become 
 // {{1,0}, {0,1}, {2,2}, {3,2}, {2,3}}

 int[][] hand = {{3,2},{2,2},{0,1},{1,0},{2,3}};
 sort(hand);

 public static void sort(int[][] hand){
    Arrays.sort(hand, new Comparator<int[]>(){
        public int compare(int[] o1, int[] o2){
            return Integer.valueOf(o1[1]).compareTo(Integer.valueOf(o2[1]));
        }
    });
 }

产出{-11,0},{0,1},{3,2},{2,2},{2,3 是否有任何建议?

最佳回答

解决办法1:将阵列按第二个要素分类,然后按第一个要素对阵列进行分类。 由于<代码>Arrays.sort是稳定的,相当于第一个要素(即第二个要素)的首次比较。

解决办法2:修改你的参照标准如下:

Arrays.sort(hand, new Comparator<int[]>() {
  public int compare(int[] o1, int[] o2) {
    if (o1[0] == o2[0]) {
      return Integer.compare(o1[1], o2[1]);
    } else {
      return Integer.compare(o1[0], o2[0]);
    }
  }
});

或者,如果瓜瓦(披露:我为瓜瓦作出贡献),你只能把参照国写成参照国。

  public int compare(int[] o1, int[] o2) {
    return ComparisonChain.start()
      .compare(o1[0], o2[0])
      .compare(o1[1], o2[1])
      .result();
  }
问题回答

你们的工作是:

int compare1 = Integer.valueOf(o1[1]).compareTo(Integer.valueOf(o2[1]);
if(compare1 != 0)
    return compare1;
else
    return Integer.valueOf(o1[0]).compareTo(Integer.valueOf(o2[0]));

我将增加 Java8的解决办法,正如有人想要知道的那样。

Arrays.sort(hand, (o1, o2) -> o1[1] == o2[1] ? Integer.compare(o1[0], o2[0]) 
                                             : Integer.compare(o1[1], o2[1]));

基本上,在第二个要素平等时,比较每个阵列的第一个要素,否则就是直接比较第二个要素。

你们可以像这样更细致地利用 Java8。

Arrays.sort(hand, Comparator.comparing(x -> ((int[])x)[1]).thenComparing(x -> ((int[])x)[0]));




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签