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

298
Vistas
Sorting an array based on a inner array

I want to sort an array based on a inner array value sorted alphabetically. example.

i want this array to be sorted by the prefix "old" so old A, old B etc

const array = [
  { personName: "Vans", personTags: ["young",  "old A"] },
  { personName: "Lia", personTags: ["young",  "ok"] },
  { personName: "Rein", personTags: ["old B", "hairless", "ok"] },
  { personName: "Cris", personTags: ["old A",  "old B", "acrounimouslyness"] },
  { personName: "Mercy", personTags: ["young", "hairless", "ok"] },
  { personName: "Cbum", personTags: ["old C", "hairless", "young"] },
];

becomes

const array = [
  { personName: "Vans", personTags: ["young",  "old A"] },
  { personName: "Cris", personTags: ["old A",  "old B", "acrounimouslyness"] },
  { personName: "Rein", personTags: ["old B", "hairless", "ok"] },
  { personName: "Cbum", personTags: ["old C", "hairless", "young"] },
];

//would be removed

{ personName: "Lia", personTags: ["young", "ok"] }, { personName: "Mercy", personTags: ["young", "hairless", "ok"] },

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You could filter then sort:

const array = [
  { personName: "Vans", personTags: ["young", "old A"] },
  { personName: "Lia", personTags: ["young", "ok"] },
  { personName: "Rein", personTags: ["old B", "hairless", "ok"] },
  { personName: "Cris", personTags: ["old B", "old A", "acrounimouslyness"] },
  { personName: "Mercy", personTags: ["young", "hairless", "ok"] },
  { personName: "Cbum", personTags: ["old C", "hairless", "young"] },
];

const getFirstOldElement = arr => {
  return arr
    .filter(s => s.startsWith("old"))
    .sort()
    .at(0);
};

const newArray = array
  .filter(
    obj =>
      (obj.personTags || []).findIndex(s => s.startsWith("old")) !== -1
  )
  .sort((a, b) =>
    getFirstOldElement(a.personTags).localeCompare(getFirstOldElement(b.personTags))
  );

console.log(newArray);

over 4 years ago · Santiago Trujillo Denunciar

0

  • Using Array#reduce, iterate over the array while updating an array of elements having an old tag. In each iteration, get the list of old tags using Array#filter and String#startsWith. If the list is not empty, push a new object to the accumulator with a special property sortedOldTags which is basically the current old tags sorted (using Array#sort, String#localeCompare, and Array#join).
  • Using Array#sort, sort the above array using the special property.
  • Using Array#map, return the sorted list taking out the special property from every element.

const array = [
  { personName: "Vans", personTags: ["young",  "old A"] },
  { personName: "Lia", personTags: ["young",  "ok"] },
  { personName: "Rein", personTags: ["old B", "hairless", "ok"] },
  { personName: "Cris", personTags: ["old A",  "old B", "acrounimouslyness"] },
  { personName: "Mercy", personTags: ["young", "hairless", "ok"] },
  { personName: "Cbum", personTags: ["old C", "hairless", "young"] },
];

const arrWithSortProp = array.reduce((arr, e) => {
  const { personTags = [] } = e;
  const oldTags = personTags.filter(tag => tag.startsWith("old"));
  if(oldTags.length) {
    arr.push({ 
      ...e, 
      sortedOldTags: oldTags.sort((a, b) => a.localeCompare(b)).join() 
    });
  }
  return arr;
}, []);

const sorted = arrWithSortProp
  .sort(({ sortedOldTags: a }, { sortedOldTags: b }) => a.localeCompare(b))
  .map(({ sortedOldTags, ...e }) => e);

console.log(sorted);

over 4 years ago · Santiago Trujillo 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