Created
April 27, 2017 20:42
-
-
Save joelpalmer/deaa1425bd33b56bacf7826fd6369d5b to your computer and use it in GitHub Desktop.
Merge 2 sorted arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const data = { | |
items: [[{ | |
name: "Joel", | |
location: "Texas", | |
Sport: "Running", | |
Id: 5 | |
}, | |
{ | |
name: "Michelle", | |
location: "California", | |
Sport: "Stroller Strides", | |
Id: 7 | |
}, | |
{ | |
name: "Liam", | |
location: "Alabama", | |
Sport: "Playing", | |
Id: 1 | |
}, | |
{ | |
name: "Dusty", | |
location: "Oregon", | |
Sport: "Fetch", | |
Id: 15 | |
} | |
], | |
[ | |
{ | |
name: "Bob", | |
location: "California", | |
Sport: "Running", | |
Id: 51 | |
}, | |
{ | |
name: "Kelly", | |
location: "New York", | |
Sport: "Stroller Strides", | |
Id: 71 | |
}, | |
{ | |
name: "Leslie", | |
location: "Washington", | |
Sport: "Playing", | |
Id: 11 | |
}, | |
{ | |
name: "Randy", | |
location: "Oregon", | |
Sport: "Fetch", | |
Id: 151 | |
} | |
], | |
], | |
} | |
const merged = (arrays) => { | |
return arrays.items.reduce((acc, curr) => { | |
return acc.concat(curr); | |
}, []).sort((a, b) => { | |
return a.Id - b.Id; | |
//for strings: a.location > b.location | |
}); | |
} | |
console.log(merged(data)); | |
//if the arrays are already sorted then you can simply concat them. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment