如何按列对2D整数数组进行排序

时间:2018-03-19 15:58:00

标签: java sorting multidimensional-array

如果我们有一个如下所示的2D int数组:

6 | 8 | 9 | 16 
0 | 6 |-3 | 4
18| 2 | 1 | 11

比预期的输出更多:

 0 | 2 |-3 | 4 
 6 | 6 | 1 | 11
 18| 8 | 9 | 16

当我想到它如何垂直排序时,我会阻止。

int[][] array = new int[10][10]; 
for(int i = 0; i < array.length; i++){
    for(int j = 0; j < array[0]; j++){
       //here i block because i don't know how i would vertically sort them
    }
}

我知道有很多关于此的主题,并且在所有这些主题中没有一个对我有用。因此,我为这篇文章道歉,但我被困住了。

3 个答案:

答案 0 :(得分:1)

您可以创建自己的类,从数组的一列创建set并使用该数组作为后备数据(即实现Collections.sort)。然后,您可以使用class ColumnList<T> extends AbstractList<T> implements List<T> { private final T[][] array; private final int column; public ColumnList(T[][] array, int column) { this.array = array; this.column = column; } @Override public T get(int index) { return array[index][column]; } @Override public T set(int index, T element) { return array[index][column] = element; } @Override public int size() { return array.length; } } public void test(String[] args) { Integer[][] array = { {6, 8, 9, 16}, {0, 6, -3, 4}, {18, 2, 1, 11} }; System.out.println("Before: " + Arrays.deepToString(array)); // Sort each column separately. for (int i = 0; i < array[0].length; i++) { ColumnList<Integer> column = new ColumnList<>(array, i); Collections.sort(column); } System.out.println("After: " + Arrays.deepToString(array)); } 对其进行现场排序。

/**
 * 
 * @param type
 * @param nodeRef
 * @return true if node type equals or inherit from type <code>type</code>
 */
protected boolean hasSubType(QName type, NodeRef nodeRef) {
    List<QName> subTypes = new ArrayList<>();
    subTypes.addAll(dictionaryService.getSubTypes(type, true));
    QName nodeType = nodeService.getType(nodeRef);
    return nodeType.equals(type) || subTypes.contains(nodeType);
}

private void getNodesRecursive(NodeRef node, List<NodeRef> documents) {
    if (hasSubType(ContentModel.TYPE_FOLDER, node)) {
        List<ChildAssociationRef> children = nodeService.getChildAssocs(node, ContentModel.ASSOC_CONTAINS, RegexQNamePattern.MATCH_ALL);
        for (ChildAssociationRef child : children) {
            NodeRef childNode = child.getChildRef();
            if (hasSubType(ContentModel.TYPE_CONTENT, node)) {
                documents.add(childNode);
            } else if (hasSubType(ContentModel.TYPE_FOLDER, node)) {
                // documents.add(childNode);
                getNodesRecursive(childNode, documents);
            }
        }
    } else {
        documents.add(node);
    }
}

打印

  

之前:[[6,8,9,16],[0,6,-3,4],[18,2,1,11]]

     

之后:[[0,2,-3,4],[6,6,1,11],[18,8,9,16]]

答案 1 :(得分:0)

请了解Linear algebra

matrix transpose

的示例
public class MatrixSort {

    private static final int MATRIX[][] = { 
              { 6, 8, 9, 16 }, 
              { 0, 6, -3, 4 }, 
              { 18, 2, 1, 11 } 
     };

    private static int[][] transpose(int[][] m) {
        int[][] ret = new int[m[0].length][m.length];
        for (int i = 0; i < m.length; i++)
            for (int j = 0; j < m[0].length; j++)
                ret[j][i] = m[i][j];
        return ret;
    }

    public static void main(String[] args) {
        int ret[][] = transpose(MATRIX);
        for(int i=0; i < ret.length; i++) {
            Arrays.sort(ret[i]);
        }
        ret = transpose(ret);
        for(int i=0; i < ret.length; i++) {
            for(int j=0; j < ret[i].length; j++) {
                System.out.print(ret[i][j]);
                System.out.print(" | ");
            }
            System.out.print('\n');
        }
    }

}

答案 2 :(得分:0)

您可以使用包含列的临时数组,并对其使用方法排序。

int[][] input = new int[numberOfRow][numberOfColumn]; 
int[][] result = new int[numberOfRow][numberOfColumn];
for(int col = 0; col < numberOfColumn; col++){
    for(int row = 0; row < numberOfRow; row++){
       int[] temp = int[numberOfRow];
       temp[row] = input[row][col];
    }
    Arrays.sort(temp);
    for(int i=0; i<numberOfColumn; i++){
        result[i][col] = temp[i];
    }
}
相关问题