31 lines
851 B
JavaScript
31 lines
851 B
JavaScript
// example 1 - sorting strings
|
|
const names = ["abcd", "ztdef", "rtzu", "hfjas", "bdfgt", "oirjk", "ljklk"]
|
|
const sortedNames = names.sort();
|
|
|
|
// example 2 - sorting numbers
|
|
const prices = [20, 25, 30, 45, 50, 90, 15];
|
|
const sortedPrices = prices.sort();
|
|
|
|
// example 3 - sorting objects
|
|
const players = [
|
|
{ name: "yoshi", score: 50 },
|
|
{ name: "mario", score: 30 },
|
|
{ name: "david", score: 18 },
|
|
{ name: "cill-on", score: 99 },
|
|
{ name: "domen", score: 50 },
|
|
{ name: "nika", score: 90 },
|
|
{ name: "peter", score: 30 }
|
|
];
|
|
|
|
const sortedPlayers = players.sort((curr, next) => {
|
|
if (curr.score > next.score) {
|
|
return 1;
|
|
} else if (curr.score < next.score) {
|
|
return -1;
|
|
} else { return 0; }
|
|
});
|
|
|
|
|
|
console.log(sortedNames)
|
|
console.log(sortedPrices)
|
|
console.log(sortedPlayers) |