Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

266
Vistas
How to sort multidimensional array by multiple column index

I want to sort multidimensional array by multiple column index.

For example, I have below test data,

var source = [
  ["Jack","A","B1", 4],
  ["AVicky","M", "B2", 2],
  ["AVicky","F", "B3", 3],
];

I want to sort source based on dynamic column index, like sometime I want to sort based on first+second, next time, I may want to sort by first+second+three.

Currently, I tried code below, but, it will only sort based on specific column index.

var source = [
  ["Jack","A","B1", 4],
  ["AVicky","M", "B2", 2],
  ["AVicky","F", "B3", 3],
];
var target = [  
  ["Tom","M", "B3", 2],
  ["Jack","F", "B1", 1],
  ["Cindy","F", "B3", 3],
];
var keyIndexs = [0,1];
var same = [];
//order rows
var sourceOrder =  source
keyIndexs.forEach(i => sourceOrder = sortByColumn(sourceOrder, i)) ;
console.log(sourceOrder);
for(var i = 0; i < source.length; i ++){
  //console.log(ContainsRow(source[i], target));
  if(ContainsRow(source[i], target)){
    same.push(source[i]);
  }
}
console.log(same);

function CompareRow(source:any[], target:any[]):boolean{
  return JSON.stringify(source) === JSON.stringify(target);
}

function ContainsRow(source:any[], target: any[][]):boolean{
  for(var i = 0; i <target.length; i ++){
    if(CompareRow(source, target[i])){
      return true;
    }
  } 
  return false;
}

function sortByColumn(a, colIndex){
  a.sort(sortFunction);
  function sortFunction(a, b) {
      if (a[colIndex] === b[colIndex]) {
          return 0;
      }
      else {
          return (a[colIndex] < b[colIndex]) ? -1 : 1;
      }
  }
  return a;
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can do:

const sortCompareFn = sortArr => (a, b) => {
  const getValue = v => sortArr.reduce((a, c) => a + v[c], '')
  const aValue = getValue(a)
  const bValue = getValue(b)
  return typeof aValue === 'string'
    ? aValue.localeCompare(bValue)
    : aValue - bValue
}

const source = [
  ["Jack","A","B1", 4],
  ["AVicky","M", "B2", 2],
  ["AVicky","F", "B3", 3],
]

const sortArr1 = [1]
const result1 = source.sort(sortCompareFn(sortArr1))
console.log('result1:', result1)

const sortArr23 = [2, 3]
const result23 = source.sort(sortCompareFn(sortArr23))
console.log('result23:', result23)

const sortArr3 = [3]
const result3 = source.sort(sortCompareFn(sortArr3))
console.log('result3:', result3)
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda