2

I have an array of objects

array = [
    {id: 5, name: "Helen", age: 20}, 
    {id: 15, name: "Lucy", age: 30}, 
    {id:7, name: "Carlos", age: 1}
]

Then I have a similar array sorted differently

arraySorted = [        
    {id: 15, name: "Lucy", age: 2}, 
    {id: 5, name: "Lara", age: 11}, 
    {id:7, name: "Carlos", age: 10}
]

The ids of the objects on both arrays will always match, the rest of the properties may or may not.

What I need is sorting the array in the same id order as arraySorted.

(it can be also done on plain JavaScript, lodash is not necessary but maybe it will be useful)

6 Answers 6

3

See Map and Array.prototype.map() for more info.

// Raw.
const raw = [
    {id: 5, name: "Helen", age: 20}, 
    {id: 15, name: "Lucy", age: 30}, 
    {id:7, name: "Carlos", age: 1}
]

// Sorted.
const sorted = [        
    {id: 15, name: "Lucy", age: 2}, 
    {id: 5, name: "Lara", age: 11}, 
    {id:7, name: "Carlos", age: 10}
]

// Match.
const match = (raw, sorted) => (m => sorted.map(s => m.get(s.id)))(new Map(raw.map(r => [r.id, r])))

// Output.
const output = match(raw, sorted)

// Proof.
console.log(output)

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

4 Comments

This is a superior solution compared to those using find (+1).
@trincot why is it superior
Array.prototype.find() executes a blind search every time it is called. Map.get() retrieves values using set keys. These attributes heavily influence time complexity.
Other solutions use a main loop with forEach or map like here, but then they have an implicit nested loop inside it by calling find which will start searching the array from left to right, meaning find runs with a O(n) time complexity. The overall time complexity thus becomes O(n²). Here, on the other hand there are three, not-nested loops (one loop runs after the other completes): two Array#map calls and an implicit iteration with the new Map call. m.get runs in constant time, so the overall time complexity is O(n+n+n) = O(n).
1

Rather than sort, use map, find and Object.assign instead

arraySorted.map( s => Object.assign( s, array.find( t => t.id == s.id ) ) );

Demo

var array = [{
    id: 5,
    name: "Helen",
    age: 20
  },
  {
    id: 15,
    name: "Lucy",
    age: 30
  },
  {
    id: 7,
    name: "Carlos",
    age: 1
  }
];
var arraySorted = [{
    id: 15,
    name: "Lucy",
    age: 2
  },
  {
    id: 5,
    name: "Lara",
    age: 11
  },
  {
    id: 7,
    name: "Carlos",
    age: 10
  }
];
array = arraySorted.map(s => Object.assign(s, array.find(t => t.id == s.id)));

console.log(array);

Comments

1

The new array can be obtained via Array.prototype.map:

const newArray = arraySorted.map(sortedItem => 
  array.find(item => item.id === sortedItem.id)
)

console.log(newArray)

Comments

1

You can use a Map, and avoid using nested iteration methods (like find, which make the solution have a worse time complexity), assuming that indeed you have all the same id values in both arrays:

const array = [{id: 5, name: "Helen", age: 20}, {id: 15, name: "Lucy", age: 30}, {id:7, name: "Carlos", age: 1}],
    arraySorted = [{id: 15, name: "Lucy", age: 2},{id: 5, name: "Lara", age: 11},{id:7, name: "Carlos", age: 10}]

const result = arraySorted.map((map => row => array[map.get(row.id)])
                               (new Map(array.map((row, i) => [row.id, i]))));

console.log(result);

1 Comment

Yes, I have the same ids all the time.
1

Try this, hope this helps

var array=[{id:5,name:"Helen",age:20},{id:15,name:"Lucy",age:30},{id:7,name:"Carlos",age:1}];

var arraySorted=[{id:15,name:"Lucy",age:2},{id:5,name:"Lara",age:11},{id:7,name:"Carlos",age:10}];

array = arraySorted.map(s => array.find(t => t.id == s.id));

console.log(array);

Comments

1

You can use Map to sort your array:

let a1 = [{id: 5, name: "Helen", age: 20}, {id: 15, name: "Lucy", age: 30}, {id:7, name: "Carlos", age: 1}],
    a2 = [{id: 15, name: "Lucy", age: 2}, {id: 5, name: "Lara", age: 11}, {id:7, name: "Carlos", age: 10}];

let map = ((m) => (
             a2.forEach(({id}) => m.set(id, a1.find(o => o.id === id))), m
          ))(new Map());

let result = [...map.values()];

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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.