Suppose we have two arrays of objects named per
and con
. The goal is to sort the per
array in ascending order based on the continent
values from the con
array. How can we achieve this? By using the sort and find functions exclusively. Below is the code snippet:
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));