728x90
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sortByAge(arr) { | |
arr.sort((a, b) => a.age - b.age); | |
} | |
let john = { name: "John", age: 25 }; | |
let pete = { name: "Pete", age: 30 }; | |
let mary = { name: "Mary", age: 28 }; | |
let arr = [ pete, john, mary ]; | |
sortByAge(arr); | |
// now: [john, mary, pete] | |
console.log(arr[0].name); // John | |
console.log(arr[1].name); // Mary | |
console.log(arr[2].name); // Pete |
'Algorithm > JavaScript(Node.js)' 카테고리의 다른 글
The Modern JS Tutorial: array-methods: Get average age (0) | 2021.09.22 |
---|---|
The Modern JS Tutorial: array-methods: shuffle an array (0) | 2021.09.21 |
The Modern JS Tutorial: array-methods: map to objects (0) | 2021.09.20 |
The Modern JS Tutorial: array-methods: map to names (0) | 2021.09.20 |
The Modern JS Tutorial: array-methods: calculator (0) | 2021.09.20 |