解法:
function solution(lines) {
const list = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const [country, gold, silver, bronze] = line.split(" ");
list.push({
country,
gold: gold - 0,
silver: silver - 0,
bronze: bronze - 0,
});
}
list.sort((a, b) => {
return a.gold !== b.gold
? b.gold - a.gold
: a.silver !== b.silver
? b.silver - a.silver
: a.bronze !== b.bronze
? b.bronze - a.bronze
: a.name - b.name;
});
return list.map((item) => item.country).join("\n");
}
console.log(
solution([
"China 32 28 34",
"England 12 34 22",
"France 23 33 2",
"Japan 12 34 25",
"Rusia 23 43 0",
])
);
/*
5
China 32 28 34
England 12 34 22
France 23 33 2
Japan 12 34 25
Rusia 23 43 0
=>
China
Rusia
France
Japan
England
*/