0

i want to sort an array of objects having id each object using another array that only has the ids, for example:

object = [
 {id: 2, name: carlos},
 {id: 1, name: maria},
 {id: 4, name: juan},
 {id: 3, name: pepe},    //this is the array that i want to be sorted or create a copy to return it
]

    [1,2,3,4,5] //this is the array that i will use as reference to sort the first one

the final result should be:

object = [
 {id: 1, name: maria},
 {id: 2, name: carlos},
 {id: 3, name: pepe},
 {id: 4, name: juam},    //this is the array that i want to be sorted or create a copy to return it
]

im using two maps, but im always getting and array with undefined:

array_to_be_sorted.map((objects) => {
  array_reference.map((id) => {
     if (objects.id === id) {
        return {...objects}
     }
  }    
}

im using map cause think is the best way for bigs array, because im building a music player, so dont know how many tracks the does the user has

1
  • Why not use Array.prototype.sort()? Sorting arrays is what it's good at. Commented Jan 4, 2021 at 17:57

3 Answers 3

2

You could use Array.prototype.sort() method to get the result.

const data = [
  { id: 2, name: 'carlos' },
  { id: 1, name: 'maria' },
  { id: 4, name: 'juan' },
  { id: 3, name: 'pepe' },
];

const order = [1, 2, 3, 4, 5];
data.sort((x, y) => order.indexOf(x.id) - order.indexOf(y.id));
console.log(data);

Another solution using Map Object which is faster than the first one.

const data = [
  { id: 2, name: 'carlos' },
  { id: 1, name: 'maria' },
  { id: 4, name: 'juan' },
  { id: 3, name: 'pepe' },
];

const order = [1, 2, 3, 4, 5];
const map = new Map();
order.forEach((x, i) => map.set(x, i));
data.sort((x, y) => map.get(x.id) - map.get(y.id));
console.log(data);

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for adding the map method, i want the faster way possible for big arrays
0

Why not just use Array.prototpye.sort()? It's easy and fast.

const pre = document.querySelector('pre');

let object = [
  {id: 2, name: 'carlos'},
  {id: 1, name: 'maria'},
  {id: 4, name: 'juan'},
  {id: 3, name: 'pepe'}
];

const criteria = [1,2,3,4,5];

pre.innerText = 'object:' + JSON.stringify(object, null, 2) + '\n\n';

object.sort((a, b) => {
  return criteria[a.id] - criteria[b.id];
});

pre.innerText += 'sorted object:' + JSON.stringify(object, null, 2);
Sort an array using criteria from a second array:

<pre></pre>

Comments

0

You can take advantage of Schwartzian transform and sort data based on another array.

const data = [ { id: 2, name: 'carlos' }, { id: 1, name: 'maria' }, { id: 4, name: 'juan' }, { id: 3, name: 'pepe' }, ],
      order = [4, 2, 3, 1, 5],
      result = data.map(o => {
                const index = order.indexOf(o.id);
                return [index, o];
              })
              .sort((a, b) => a[0] - b[0])
              .map(([, o]) => o);
console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.