views:

46

answers:

2

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. 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?

A: 

This page seems like it would help:

http://www.go4expert.com/forums/showthread.php?t=8158

spinon
+3  A: 

I'd create a class that has attributes i, j and value. Create an object for each value in the matrix by filling i, j and the matrix value into this object. 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.

Then print the (i,j) pairs in the sorted list.

David Sauter