Let per
& con
be the arrays of objects. I want to sort the per
array as per the ascending order of continent
of con
array. What to do? Just use sort and find functions only. Here's my code:
function checkContinent(pe, co) {
const cc = pe.sort(function(a) {
let fa = co.find(function(b) {
return b.city === a.city;
});
fa.sort(sortContAsc);
return fa;
});
return cc;
}
function sortContAsc(s1, s2) {
return s1.continent.localeCompare(s2.continent);
}
const per = [{
name: "Mary",
city: "London"
},
{
name: "Anita",
city: "Paris"
},
{
name: "Edward",
city: "New York"
},
{
name: "Thomas",
city: "Rome"
},
{
name: "Robin",
city: "Seattle"
},
{
name: "Sophia",
city: "Los Angeles"
},
{
name: "Bruce",
city: "Delhi"
}
];
const con = [{
city: "London",
continent: "Europe"
},
{
city: "Delhi",
continent: "Asia"
},
{
city: "Seattle",
continent: "North America"
},
{
city: "Paris",
continent: "Europe"
},
{
city: "New York",
continent: "North America"
},
{
city: "Rome",
continent: "Europe"
},
{
city: "Bengaluru",
continent: "Asia"
},
{
city: "Los Angeles",
continent: "North America"
}
];
const cs = con.sort(sortContAsc);
console.log(checkContinent(per, cs));