Question: 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)

Answer: 
See [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) and [`Array.prototype.map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) for more info.

```js
// 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)
```
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:

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