var shanhai = { name: "Шанхай", population: 23416 };
var jakarta = { name: "Джакарта", population: 30539 };
var pekin = { name: "Пекин", population: 21009 };
var deli = { name: "Дели", population: 24998 };
var manila = { name: "Манила", population: 24123 };
var cities = [shanhai, jakarta, pekin, deli, manila];
function sort(city1, city2) {
return city2.population - city1.population; //Заменим местами 1 и 2 и получим уже по возрастанию.
}
function populationSort(arr, x) {
var newArr = arr.sort(sort);
newArr = newArr.slice(0, x);
var newNewArr = []; //newNewArr :)
for (var i = 0; i < x; i++) {
newNewArr[i] = newArr[i].name;
};
return newNewArr;
}
console.log(populationSort(cities, 5));
dmFyIHNoYW5oYWkgPSB7IG5hbWU6ICLQqNCw0L3RhdCw0LkiLCBwb3B1bGF0aW9uOiAyMzQxNiB9Owp2YXIgamFrYXJ0YSA9IHsgbmFtZTogItCU0LbQsNC60LDRgNGC0LAiLCBwb3B1bGF0aW9uOiAzMDUzOSB9Owp2YXIgcGVraW4gPSB7IG5hbWU6ICLQn9C10LrQuNC9IiwgcG9wdWxhdGlvbjogMjEwMDkgfTsKdmFyIGRlbGkgPSB7IG5hbWU6ICLQlNC10LvQuCIsIHBvcHVsYXRpb246IDI0OTk4IH07CnZhciBtYW5pbGEgPSB7IG5hbWU6ICLQnNCw0L3QuNC70LAiLCBwb3B1bGF0aW9uOiAyNDEyMyB9OwoKdmFyIGNpdGllcyA9IFtzaGFuaGFpLCBqYWthcnRhLCBwZWtpbiwgZGVsaSwgbWFuaWxhXTsKCmZ1bmN0aW9uIHNvcnQoY2l0eTEsIGNpdHkyKSB7CglyZXR1cm4gY2l0eTIucG9wdWxhdGlvbiAtIGNpdHkxLnBvcHVsYXRpb247CQkvL9CX0LDQvNC10L3QuNC8INC80LXRgdGC0LDQvNC4IDEg0LggMiDQuCDQv9C+0LvRg9GH0LjQvCDRg9C20LUg0L/QviDQstC+0LfRgNCw0YHRgtCw0L3QuNGOLgp9CgpmdW5jdGlvbiBwb3B1bGF0aW9uU29ydChhcnIsIHgpIHsKCXZhci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