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

110
Views
How can I sort an array by another array of objects?

How can I sort an array by string?

const filter = [
  { index: "First" },
  { index: "Second" },
  { index: "Third" }
]

const data = [
  { title: 'Apple', index: "Second" },
  { title: 'Samsung', index: "First" },
  { title: 'BMW', index: "Third" },
  { title: 'Apple', index: "Second" },
  { title: 'Apple', index: "Second" },
  { title: 'Samsung', index: "First" }
]

Expected results:

const data = [
  { title: 'Samsung', index: "First" },
  { title: 'Samsung', index: "First" },
  { title: 'Apple', index: "Second" },
  { title: 'Apple', index: "Second" },
  { title: 'BMW', index: "Third" }
]

How to iterate over two arrays correctly? Do I have to iterate over two arrays to do this? Or is there another way to get the desired result?

Given three inputs - "First", "Second", "Third", if there will be more of these indices?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

  • Using Array#reduce, iterate over filter and save each object's index attribute and its index in the array in a Map
  • Using Array#sort, sort data by the values of the index attributes from the above map

const 
  filter = [ { index: "First" }, { index: "Second" }, { index: "Third" } ],
  data = [ { title: 'Apple', index: "Second" }, { title: 'Samsung', index: "First" }, { title: 'BMW', index: "Third" }, { title: 'Apple', index: "Second" }, { title: 'Apple', index: "Second" }, { title: 'Samsung', index: "First" } ];
  
const indexSortMap = filter.reduce((map, { index }, i) => map.set(index, i), new Map);

data.sort(({ index: a }, { index: b }) => indexSortMap.get(a) - indexSortMap.get(b));

console.log(data);

about 4 years ago · Juan Pablo Isaza Report

0

Use Array#sort with custom compareFunction and Array#findIndex with custom callback function

const filter = [ { index: "First",}, {index: "Second",}, {index: "Third",}];
const data = [ { title: 'Apple', index: "Second" }, { title: 'Samsung', index: "First" }, { title: 'BMW', index: "Third" }, { title: 'Apple', index: "Second" }, { title: 'Apple', index: "Second" }, { title: 'Samsung', index: "First" } ];

data.sort((a ,b) => {
    return filter.findIndex(i => i.index === a.index) - filter.findIndex(i => i.index === b.index);
});

console.log(data);

about 4 years ago · Juan Pablo Isaza Report

0

One possible solution (and simple) is by iterating these two arrays. They will be pushed according to the order of the "filter" array:

const sortedArray = []
filter.forEach(fil=> {
  data.forEach(dat=>{
    if(dat.index===fil.index)
      sortedArray.push(dat)
  })
})
console.log(sortedArray)
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!