简体   繁体   English

按值对二维矩阵排序的最佳方法是什么?

[英]What's the best way to sort a two-dimensional matrix by values?

I have a two-dimensional array: 我有一个二维数组:

function getMatrix(size) {
    var matrix = [];

    for (var i = 0; i < size; i++) {
        matrix[i] = new Array(size);
    }
    return matrix;
};

It is filled with numeric values, so every existing matrix[i][j] is a Number . 它用数值填充,因此每个现有的matrix[i][j]都是一个Number What is the best way to get a sequence of i and j pairs that will correspond to a seqence of highest to lowest values in the matrix? 获得对应于矩阵中最高值到最低值的序列的ij对的最佳方法是什么?

I'd create a class that has attributes i , j and value . 我将创建一个具有ijvalue属性的类。 Create an object for each value in the matrix by filling i, j and the matrix value into this object. 通过将i,j和矩阵值填充到该对象中,为矩阵中的每个值创建一个对象。 Put all objects in a list and sort the list with list.sort(sortFunction) and a self-defined sortFunction that sorts the list by object.value . 将所有对象放入列表中,并使用list.sort(sortFunction)和一个自定义sortFunction对列表进行排序,该object.valueobject.value对列表进行object.value

Then print the (i,j) pairs in the sorted list. 然后在已排序的列表中打印(i,j)对。

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

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