How to sort a two dimensional array by columns in Javascript?


How to sort a two dimensional array by columns in Javascript ?

For example, I have this 2D array, that is, a JSON array in table form, and I want to sort it by columns:

var matriz = [
//Columnas:   0 1 2 3 4
/*Filas: 0*/ [9,5,3,2,7],
/*       1*/ [7,9,5,4,3],
/*       2*/ [8,4,6,0,1]

Accessing the element array[2][4] returns 1 , that is, row 2 , column 4 .

What I want to do is sort, for example, column 3 , but without changing the sequence in each row, and it would look like this:

var matriz = [
//Columnas:   0 1 2 3 4
/*Filas: 0*/ [8,4,6,0,1]
/*       1*/ [9,5,3,2,7],
/*       2*/ [7,9,5,4,3],


To sort the array, I use binary search. This type of search finds a value in the fewest possible steps applied to an ordered sequence. For example, if I want to find 66 out of 100 numbers, what I do is, first, I say half of the total amount, that is, I ask if it is 50. The answer to that is, "No, it is greater than 50" , then I ask the average between 50 and 100, that is, is it 75 ?, no, it is less, and so on.

I am looking for 66 , I am getting closer and closer … 100 50 75 62 68 65 66 . This is similar to what we do unconsciously when looking up a word in a paper dictionary, nobody looks page by page, but it gets closer.

What I do to order the matrix is, first I declare an empty matrix, and I insert each row in an ordered way. In this way, the indicated column is ordered without altering the sequence in each row.

To insert a row, first I look for the indicated position to insert it, that is, the row to insert will be at the beginning of the matrix, or at the end, or otherwise between two other rows.

Also, it is possible to indicate if I want to sort the array in descending order. Otherwise it will be ascending. The compara function just compares two numbers. If the first is less than the second, it returns 1, if they are equal, it returns 0, and if the first is greater than the second, it returns -1 … All that, if it is esDescentente is false , but if it is true , the -1 is 1, and vice versa.


 var matriz = [ //Columnas: 0 1 2 3 4 /*Filas: 0*/ [9,5,3,2,7], /* 1*/ [7,9,5,4,3], /* 2*/ [8,4,6,0,1] ] function compara(uno,dos,esDescendente) { var primero=uno*=1 var segundo=dos*=1 var devuelve=0 if(primero<segundo){devuelve= -1} if(primero>segundo){devuelve= 1} if( esDescendente ){devuelve*=-1} return devuelve } function buscar(matriz,valor,columna,compara,esDescendente) { var comparado=0 var i=0 var j=matriz.length-1 var fila=Math.floor((i+j)/2) var devuelve while(i<=j) { comparado=compara(matriz[fila][columna],valor,esDescendente) if(comparado== 1){j=fila-1} if(comparado==-1){i=fila+1} if(comparado== 0){break} fila=Math.floor((i+j)/2) } array_fila=matriz[fila] if(array_fila!=undefined) { comparado=compara(array_fila[columna],valor,esDescendente) } devuelve=fila+(comparado==-1) if(devuelve<0){devuelve=0} return devuelve } function insertarOrdenado(matriz,valor,columna,compara,esDescendente) { var posición=buscar(matriz,valor[columna],columna,compara,esDescendente) var inicio=matriz.slice(0,posición) var medio=[valor] var fin=matriz.slice(posición) var devuelve=inicio.concat(medio).concat(fin) return devuelve } function ordenar(matriz,columna,compara,esDescendente) { var devuelve=[] for(var i=0;i<matriz.length;i++) { devuelve=insertarOrdenado( devuelve,matriz[i],columna,compara,esDescendente ) } return devuelve } ascendente=ordenar(matriz,3,compara,false) console.log(JSON.stringify(ascendente).replace(/\],\[|\[\[|]]/g,"\n"))
Scroll to Top