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

145
Views
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 answers
Answer question

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 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!