Home > front end >  Sort Array of Objects based on position of value in another Array of Strings
Sort Array of Objects based on position of value in another Array of Strings

Time:03-16

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.

CodePudding user response:

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);

CodePudding user response:

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
}

CodePudding user response:

i made a simple example sorting objects by their Type's index in the CORRECT_ORDER table:

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

const sorted = unsorted.sort((a,b) => {
    const indexA = order.findIndex(type => a.Type === type);
    const indexB = order.findIndex(type => b.Type === type);
  return indexA - indexB; // to get positive, 0, negative number for the sort callback.
});

console.log(sorted);

You can check its implementation in JSFiddle

CodePudding user response:

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.

  • Related