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

146
Vistas
Sort by multiple strings in Array using Javascript

I am trying to sort an array by multiple strings but coming up short. Is this possible or is writing something below the only to achieve this?

Doesn't work:

const colorArr = ["Red", "Blue", "Green"]
const colorMap = colorArr.map((a) => a);

const newArr = data.sort((a, b) => {
  return (
    (a.credits.credit.value !== colorMap) - (b.credits.credit.value !== colorMap)
  );
});

console.log("newArr========", newArr)

This works, but can get very lengthy the more conditions...

const data = [
  {
    credits: {
      credit: {
        value: "Red",
      },
    },
  },
  {
    credits: {
      credit: {
        value: "Blue",
      },
    },
  },
  {
    credits: {
      credit: {
        value: "Green",
      },
    },
  },
  {
    credits: {
      credit: {
        value: "Red",
      },
    },
  },
  {
    credits: {
      credit: {
        value: "Red",
      },
    },
  },
  {
    credits: {
      credit: {
        value: "Blue",
      },
    },
  },
];


const nameActor = "Red";
const nameEp = "Blue";
const nameDirector = "Green";

  const newArr = data.sort((a, b) => {
    return (
      (a.credits.credit.value !== nameActor) - (b.credits.credit.value !== nameActor) ||
      (a.credits.credit.value !== nameEp) - (b.credits.credit.value !== nameEp) ||
      (a.credits.credit.value !== nameDirector) - (b.credits.credit.value !== nameDirector)
    );
  });
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

const colorOrder = ['Red', 'Blue', 'Green'];

const order = data.sort(
  (a, b) =>
    colorOrder.indexOf(a.credits.credit.value) -
    colorOrder.indexOf(b.credits.credit.value)
);
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