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

140
Vistas
Ordenar función con condición múltiple

Tengo una matriz de objetos como a continuación:

 var data = [ { name": "Name2", "webOrderingEnabled": true, "distance": 1.6989125091571928 }, { "name": "Name3", "webOrderingEnabled": false, "distance": 1.9178283920396098 }, { "name": "Name4", "webOrderingEnabled": false, "shutdown": { "message": "", "status": true }, "distance": 6.94478210395609 }, { "name": "Name1", "webOrderingEnabled": true, "shutdown": { "message": "", "status": false }, "distance": 0.5368834377514055 } ]

Quiero ordenar esta matriz de objetos 1.webOrderingEnabled 2.shutdown.status = false 3. Distancia El desafío es que algún objeto no tiene la tecla de apagado si no está presente, considere que está abierto en la tienda. Intenté el siguiente enfoque, no funcionó. para mi

 data.sort((a, b) => { if (a.shutdown?.status && b.shutdown?.starus || !a.shutdown?.status && !b.shutdown?.status && a.webOrderingEnabled || b.webOrderingEnabled) { return a.distance - b.distance; } if (a.shutdown?.status) { return -1; } return 1; });

Esperado:

 [{ "name": "Name1", "webOrderingEnabled": true, "shutdown": { "message": "", "status": false }, "distance": 0.5368834377514055 }, { "name": "Name2", "webOrderingEnabled": true "distance": 1.6989125091571928 }, { "name": "Name3", "webOrderingEnabled": false, "distance": 1.9178283920396098 }, { "name": "Name4", "webOrderingEnabled": false, "shutdown": { "message": "", "status": true }, "distance": 6.94478210395609 }]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

En general, si desea ordenar una matriz de objetos de acuerdo con los campos A, B, C (en este orden), simplemente haga lo siguiente:

 if (aA < bA) return -1; else if (aA > bA) return 1; // otherwise aA == bA, so let's proceed with considering B now... if (aB < bB) return -1; else if (aB > bB) return 1; // otherwise aA == bA && aB == bB, so let's proceed with considering C now... if (aC < bC) return -1; else if (aC > bC) return 1; return 0;

Aquí puede pensar en aA < bA como un pseudocódigo para comparar ese valor de atributo único, exactamente cómo se hace eso depende de los tipos.

Así que tu código debería verse algo como

 // I assume by "consider to be store open" you mean shutdown.status === false function getShutdownStatus(a) { return a?.shutdown?.status || false; } data.sort((a, b) => { // Use ! here because true > false but it seems you want true to come first if (!a.webOrderingEnabled < !b.webOrderingEnabled) return -1; else if (!a.webOrderingEnabled > !b.webOrderingEnabled) return 1; const aShutdown = getShutdownStatus(a); const bShutdown = getShutdownStatus(b); if (aShutdown < bShutdown) return -1; else if (aShutdown > bShutdown) return 1; // otherwise compare by distance if (a.distance < b.distance) return -1; else if (a.distance > b.distance) return 1 return 0; });

Una vez que tenga esto funcionando, puede simplificar el código:

 data.sort((a, b) => { if (a.webOrderingEnabled != b.webOrderingEnabled) return b.webOrderingEnabled - a.webOrderingEnabled; const aShutdown = getShutdownStatus(a); const bShutdown = getShutdownStatus(b); if (aShutdown != bShutdown) return aShutdown - bShutdown; return a.distance - b.distance; });
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