Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

265
Views
Clasificación por orden predefinido con múltiples propiedades

Quiero ordenar a los asistentes según los groups clave, que es una matriz de grupos (función como etiquetas). Quiero ordenar por el orden de clasificación predefinido y luego por si sortOrder es verdadero si existen varios asistentes para el mismo grupo. Pero estoy realmente atascado con la forma de verificar si existe un nombre de grupos y luego clasificarlos de manera eficiente por el orden predefinido.

 const attendees = [ { name: "Peter", groups: [{ name: "agency", main_contact: false, stand_in_contact: true }], }, { name: "Alex", groups: [{ name: "agency", main_contact: true, stand_in_contact: false }], }, { name: "Nina", groups: [ { name: "production", main_contact: true, stand_in_contact: false }, ], }, { name: "Christina", groups: [ { name: "client", main_contact: true, stand_in_contact: false }, { name: "crew", main_contact: false, stand_in_contact: false }, ], }, ]; const sortAttendees = (attendees) => { const sortOrder = ["agency", "client", "production", "crew"]; // return sortOrder.indexOf(a.type) - sortOrder.indexOf(b.type); if (!attendees || attendees.length == 0) return []; return attendees.sort((a, b) => { if (a.groups.some((group) => group.name === "agency")) { return -1; } if (a.groups.some((group) => group.name === "client")) { return 1; } if (a.groups.some((group) => group.name === "production")) { return 1; } return 1; }); };

Resultado

 const attendees = [ { name: "Alex", groups: [{ name: "agency", main_contact: true, stand_in_contact: false }], }, { name: "Peter", groups: [{ name: "agency", main_contact: false, stand_in_contact: true }], }, { name: "Christina", groups: [ { name: "client", main_contact: true, stand_in_contact: false }, { name: "crew", main_contact: false, stand_in_contact: false }, ], }, { name: "Nina", groups: [ { name: "production", main_contact: true, stand_in_contact: false }, ], }, ];
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Parece que los grupos deben ordenarse primero, luego puede ordenar a los asistentes.

 const attendees = [ { name: "Peter", groups: [{ name: "agency", main_contact: false, stand_in_contact: true }], }, { name: "Alex", groups: [{ name: "agency", main_contact: true, stand_in_contact: false }], }, { name: "Nina", groups: [ { name: "production", main_contact: true, stand_in_contact: false }, ], }, { name: "Christina", groups: [ { name: "client", main_contact: true, stand_in_contact: false }, { name: "crew", main_contact: false, stand_in_contact: false }, ], }, ]; const sortOrder = ["agency", "client", "production", "crew"]; const sortAttendees = (attendees, sortOrder) => { if ([attendees, sortOrder].some(a => !(a instanceof Array))) return; // first, sort the groups: attendees.forEach(attendee => attendee.groups.sort((a, b) => sortOrder.indexOf(a.name) - sortOrder.indexOf(b.name) || b.main_contact - a.main_contact)); // now sort attendees: attendees.sort((a, b) => { const l = Math.min(a.groups.length, b.groups.length); for (let i = 0; i < l; ++i) { const r = sortOrder.indexOf(a.groups[i].name) - sortOrder.indexOf(b.groups[i].name) || b.groups[i].main_contact - a.groups[i].main_contact; if (r) return r; } return 0; }); }; sortAttendees(attendees, sortOrder); console.log( attendees )
 .as-console-wrapper {top:0; max-height: 100% !important}

about 4 years ago · Juan Pablo Isaza Report

0

En mi solución, doy a cada asistente un valor de clasificación en función de su membresía con la clasificación más alta en un grupo y su estado de contacto principal en ese grupo.

 const attendees = [ { name: "Peter", groups: [{ name: "agency", main_contact: false, stand_in_contact: true }], }, { name: "Alex", groups: [{ name: "agency", main_contact: true, stand_in_contact: false }], }, { name: "Nina", groups: [ { name: "production", main_contact: true, stand_in_contact: false }, ], }, { name: "Christina", groups: [ { name: "client", main_contact: true, stand_in_contact: false }, { name: "crew", main_contact: false, stand_in_contact: false }, ], }, ]; const sortAttendees = (attendees) => { const sortOrder = ["agency", "client", "production", "crew"]; const rank = attendee => attendee.groups .map(group => sortOrder.indexOf(group.name)*2 + 1 - group.main_contact) .sort() [0]; if (!attendees || attendees.length == 0) return []; return attendees.sort((a, b) => rank(a)-rank(b)); }; console.log(sortAttendees(attendees));
 .as-console-wrapper {top:0; max-height: 100% !important}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!