根据另一个字符串数组对2D字符串数组进行排序-Java

时间:2019-03-15 21:00:20

标签: java arrays sorting

正如标题所述,我希望实现一种方法,该方法将允许我基于数组(例如:根据数组的顺序按第2列对2d数组进行排序。

String[][] a1 = new String[][]{
  {x,dog,x}
  {x,monkey,x}
  {x,cat,x}
}
//x being a random string

String[] a2 = new String[]{dog,cat,monkey};

//Output should be:
 a1 = {
  {x,dog,x}
  {x,cat,x}
  {x,monkey,x}
 }

我不确定从这个角度看要走什么方向,我已经看过比较器,但不确定在这种情况下如何实现。任何帮助我实现这一目标的指导表示赞赏。

4 个答案:

答案 0 :(得分:0)

您需要一个取决于Comparator<String[]>数组的a2

String[] a2 = new String[]{"dog", "cat", "monkey"};

Comparator<String[]> comparator = (arr1, arr2) -> {
    List<String> list = Arrays.asList(a2);

    int index1 = list.indexOf(arr1[1]);
    int index2 = list.indexOf(arr2[1]);

    return Integer.compare(index1, index2);
};

Arrays.sort(a1, comparator);
System.out.println(Arrays.deepToString(a1));

答案 1 :(得分:0)

假设猫,狗和猴子为String对象:

  Arrays.stream(a1).sorted(Comparator.comparingInt(s -> -1 * Arrays.asList(a2).indexOf(s[1]))).toArray(String[][]::new);

答案 2 :(得分:0)

扩展@Ruslan答案,比较器是正确的方法,但是您可能不想为排序中的每个比较都做一个indexof,因此您想从将参考字符串映射到反向索引开始:

    String[] a2 = new String[]{"dog", "cat", "monkey"};
    Map<String, Integer> order = new HashMap<>();
    for(int i = 0; i < a2.length; i++) {
      order.put(a2[i], i);
    }

    Comparator<String[]> comparator = (arr1, arr2) -> {
      int index1 = order.get(arr1[1]);
      int index2 = order.get(arr2[1]);

      return Integer.compare(index1, index2);
    };

    Arrays.sort(a1, comparator);

答案 3 :(得分:0)

以下代码段应为您提供帮助。 它按照字符串2的顺序对数组进行排序,顺序为a2提供。如果秒字符串相同,则比较第一个字符串,然后比较第三个字符串。 请注意,它确实会检查空字符串。

重要的是:

 @Override
    public int compare(String[] o1, String[] o2) {      
        int score1 = types.indexOf(o1[1])-types.indexOf(o2[1]);
        if(score1!=0) {
            return score1;
        }

        int score2 = o1[0].compareTo(o2[0]);
        if(score2!=0) {
            return score2;
        }

        int score3 = o1[2].compareTo(o2[2]);
        return score3;
    }

请参阅:

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

public class SpecialComparatorMain implements Comparator<String[]> {

    private List<String> types;

    public SpecialComparatorMain(List<String> types) {
        this.types = types;
    }

    public static void main(String[] args) {
        String[] a2 = new String[]{"dog","cat","monkey"};
        String[][] a1 = new String[][]{
              {"z","dog","x"},
              {"y","monkey","x"},
              {"x","cat","x"},
              {"x","monkey","x"},
              {"y","cat","x"},
              {"z","monkey","z"},
              {"z","monkey","y"},
              {"z","monkey","x"},
            };
            //x being a random string

            System.out.println("Before");
            Stream.of(a1).map(Arrays::toString).forEach(System.out::println);

            Arrays.sort(a1, new SpecialComparatorMain(Arrays.asList(a2)));

            System.out.println("After");
            Stream.of(a1).map(Arrays::toString).forEach(System.out::println);

    }

    @Override
    public int compare(String[] o1, String[] o2) {      
        int score1 = types.indexOf(o1[1])-types.indexOf(o2[1]);
        if(score1!=0) {
            return score1;
        }

        int score2 = o1[0].compareTo(o2[0]);
        if(score2!=0) {
            return score2;
        }

        int score3 = o1[2].compareTo(o2[2]);
        return score3;
    }

}

输出:

Before
[z, dog, x]
[y, monkey, x]
[x, cat, x]
[x, monkey, x]
[y, cat, x]
[z, monkey, z]
[z, monkey, y]
[z, monkey, x]

After
[z, dog, x]
[x, cat, x]
[y, cat, x]
[x, monkey, x]
[y, monkey, x]
[z, monkey, x]
[z, monkey, y]
[z, monkey, z]

HTH!