有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

排序如何在Java中按一列对二维数组进行排序

所以我想要的是这个

int[]arr=新int[2][8]

输入:

1 1 3 1 5 3 7 1

524483752

输出:

1 1 5 3 1 7 3 1

2 2 3 4 5 7 8

您可以看到它是按第二行升序排序的,第一行紧跟其后, 我该怎么做?请帮忙

我试着在下面做

数组。排序(arr[1])

但我认为它不起作用。它确实按升序对第二行进行排序,但第一行与第二行的初始对不匹配


共 (2) 个答案

  1. # 1 楼答案

    试试这个

    public static void main(String[] args) {
        int[][] array = {
            {1, 1, 3, 1, 5, 3, 7, 1},
            {5, 2, 4, 8, 3, 7, 5, 2}
        };
    
        List<int[]> list = new AbstractList<int[]>() {
    
            @Override
            public int[] get(int index) {
                return new int[] {array[1][index], array[0][index]};
            }
    
            @Override
            public int[] set(int index, int[] value) {
                int[] old = get(index);
                array[1][index] = value[0];
                array[0][index] = value[1];
                return old;
            }
    
            @Override
            public int size() {
                return array[0].length;
            }
        };
    
        Collections.sort(list, Arrays::compare);
    
        for (int[] row : array)
            System.out.println(Arrays.toString(row));
    }
    

    输出:

    [1, 1, 5, 3, 1, 7, 3, 1]
    [2, 2, 3, 4, 5, 5, 7, 8]
    

    或者

    public static void main(String[] args) {
        int[][] array = {
            {1, 1, 3, 1, 5, 3, 7, 1},
            {5, 2, 4, 8, 3, 7, 5, 2}
        };
    
        int[] sortedIndexes = IntStream.range(0, array[0].length)
            .boxed()
            .sorted(Comparator.comparing((Integer i) -> array[1][i])
                .thenComparing(i -> array[0][i]))
            .mapToInt(Integer::intValue)
            .toArray();
    
        int[][] output = IntStream.range(0, array.length)
            .mapToObj(r -> IntStream.range(0, array[r].length)
                .map(i -> array[r][sortedIndexes[i]])
                .toArray())
            .toArray(int[][]::new);
    
        for (int[] r : output)
            System.out.println(Arrays.toString(r));
    }
    
  2. # 2 楼答案

    可以使用助手方法对输入数组进行转置,然后按列对转置后的数组进行排序,然后再次转置以恢复原始行/列:

    // create new array to store transposed
    public static int[][] transpose(int[][] src) {
        return transpose(src, new int[src[0].length][src.length]);
    }    
    
    // use existing array to store the transposed
    public static int[][] transpose(int[][] src, int[][] dst) {
        for (int i = 0, n = src.length; i < n; i++) {
            for (int j = 0, m = src[i].length; j < m; j++) {
                dst[j][i] = src[i][j];
            }
        }
        return dst;
    }
    

    方法sortByColumn(重用输入数组):

    public static void sortByColumn(int[][] arr, Comparator<int[]> comparator) {
        int[][] toSort = transpose(arr);
        Arrays.sort(toSort, comparator);
        transpose(toSort, arr);
    }
    

    测试:

    int[][] arr = {
        {7, 1, 3, 1, 5, 3, 1, 4, 4},
        {5, 2, 4, 8, 3, 7, 5, 2, 5}
    };
    
    sortByColumn(arr, Comparator.comparingInt(col -> col[1]));
    
    for (int[] row : arr) {
        System.out.println(Arrays.toString(row));
    }
    

    输出: 在第一行中,值在按每列中的第二个元素排序后按插入顺序显示

    [1, 4, 5, 3, 7, 1, 4, 3, 1]
    [2, 2, 3, 4, 5, 5, 5, 7, 8]
    

    方形阵列(宽度=高度)可以更有效地进行转置,而无需创建额外的阵列:

    public static int[][] transposeSquare(int[][] arr) {
        for (int i = 0, n = arr.length; i < n; i++) {
            // select the elements only above the main diagonal
            for (int j = i + 1, m = arr[i].length; j < m; j++) {
                int tmp = arr[i][j];
                arr[i][j] = arr[j][i];
                arr[j][i] = tmp;
            }
        }
        return arr;
    }