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

107
Views
Sort Array of Objects based on position of value in another Array of Strings

I have one array:

const CORRECT_ORDER = ['Animal','Plant','Sand','Grass'];

Then I have another array of Objects:

const UNSORTED = [{Type: 'Grass', Value: 'Wet'}, {Type: 'Sand', Value: 'Dry'}, {Type: 'Animal', Value: 'Dog'}];

I want to sort the UNSORTED array so that the Animal type comes first, followed by Plant then Sand and Grass.

If the CORRECT_ORDER array changes order I should be able to resort the UNSORTED array to match the new order.

It is safe to assume that no Types (Grass, Sand, Plant, Animal) will repeat and that type will only show up once in the unsorted array, if at all.


I have tried something like the following: PSUEDO CODE:

const SORTED = [];
UNSORTED.ForEach(value){
 const positionIndex = CORRECT_ORDER.indexOf(value.Type);

 if(positionIndex > SORTED.length){
   //Push at end
   SORTED.push(value);
 } else {
   //Push at index
   SORTED.splice(positionIndex, 0, value);
 }
}

return SORTED;

Unfortunately this isn't foolproof and it often sorts things incorrectly, especially on datasets that are a big larger.

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

0

const CORRECT_ORDER = ['Animal','Plant','Sand','Grass'];

const UNSORTED = [{Type: 'Grass', Value: 'Wet'}, {Type: 'Sand', Value: 'Dry'}, {Type: 'Animal', Value: 'Dog'}];


function sort_objects(order, unsortedArray){

    let newArray = Array();

    for(i = 0; i < order.length; i++){

        for(j = 0; j < unsortedArray.length; j++){

            if(unsortedArray[j].Type == order[i]){
                newArray.push(unsortedArray[j]);
                break;
            }
        }

    }

    return newArray

}


console.log(sort_objects(CORRECT_ORDER, UNSORTED))

this might work but it can be made more efficient.

about 4 years ago · Juan Pablo Isaza Report

0

You can loop the correct_order array and filter the unsorted array by using the js filter function. If filter match push to an new array.

const UNSORTED = [{Type: 'Grass', Value: 'Wet'}, {Type: 'Sand', Value: 'Dry'}, {Type: 'Animal', Value: 'Dog'}];

const CORRECT_ORDER = ['Animal','Plant','Sand','Grass'];

let sorted = []
CORRECT_ORDER.forEach(k => {
  let n = UNSORTED.filter(obj => {
    return obj.Type === k
  })
  if (n.length > 0) {
    sorted.push(n);  
  }
  
})

console.log(sorted);

about 4 years ago · Juan Pablo Isaza Report

0

Try this

function sort() {
 const map = {}
 CORRECT_ORDER.map((type, i) => (map[type] = i))
 const sortedArr = UNSORTED.sort((a, b) => map[a.Type] - map[b.Type])
 return sortedArr
}
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!