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

229
Vistas
Sorting within another sort

I have a set of data that has multiple properties, and the primary way I want them sorted is having item B after item A, but I don't want to affect the order of those items outside of that.

Dataset (in no particular order):

  • record 1
  • record 2
  • record 3 (has Item A property)
  • record 4
  • record 5 (item A)

Dataset (sorted by item A)

  • record 1
  • record 2
  • record 4
  • record 5 (item A)
  • record 3 (has Item A property)

My goal is to sort whatever has the "item A property" after the actual "item A". I'm blanking on how to make sure it's always sorted below the other item, regardless of what order each item returns.

I'm trying to do this within a JavaScript sort function, if possible.

Here's another example as well:

Dataset (in no particular order):

  • record 1
  • record 2 (item A)
  • record 3
  • record 4
  • record 5 (has Item A property)

Dataset (sorted by item A)

  • record 1
  • record 2 (item A)
  • record 5 (has Item A property)
  • record 3
  • record 4
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could map the items and for the wanted group take an array. later flat the result.

const
    array = ['b', 'c', 'd', 'e', 'aa', 'a'],
    group = [],
    result = array
        .map(v => {
            if (v === 'a') {      // first item
                group.unshift(v);
                return group;
            }
            if (v === 'aa') {     // connected item
                group.push(v);
                return [];
            }
            return v;
        })
        .flat();

console.log(...result);

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