components
This commit is contained in:
parent
304d578b54
commit
6fb4aabcba
344 changed files with 669 additions and 406 deletions
|
@ -1,5 +1,5 @@
|
|||
```js run no-beautify
|
||||
function sortByName(arr) {
|
||||
function sortByAge(arr) {
|
||||
arr.sort((a, b) => a.age > b.age ? 1 : -1);
|
||||
}
|
||||
|
||||
|
@ -7,11 +7,12 @@ let john = { name: "John", age: 25 };
|
|||
let pete = { name: "Pete", age: 30 };
|
||||
let mary = { name: "Mary", age: 28 };
|
||||
|
||||
let arr = [ john, pete, mary ];
|
||||
let arr = [ pete, john, mary ];
|
||||
|
||||
sortByName(arr);
|
||||
sortByAge(arr);
|
||||
|
||||
// now sorted is: [john, mary, pete]
|
||||
alert(arr[0].name); // John
|
||||
alert(arr[1].name); // Mary
|
||||
alert(arr[2].name); // Pete
|
||||
```
|
||||
|
|
|
@ -2,9 +2,9 @@ importance: 5
|
|||
|
||||
---
|
||||
|
||||
# Sort objects
|
||||
# Sort users by age
|
||||
|
||||
Write the function `sortByName(users)` that gets an array of objects with the `age` property and sorts them by `age`.
|
||||
Write the function `sortByAge(users)` that gets an array of objects with the `age` property and sorts them by `age`.
|
||||
|
||||
For instance:
|
||||
|
||||
|
@ -13,11 +13,12 @@ let john = { name: "John", age: 25 };
|
|||
let pete = { name: "Pete", age: 30 };
|
||||
let mary = { name: "Mary", age: 28 };
|
||||
|
||||
let arr = [ john, pete, mary ];
|
||||
let arr = [ pete, john, mary ];
|
||||
|
||||
sortByName(arr);
|
||||
sortByAge(arr);
|
||||
|
||||
// now: [john, mary, pete]
|
||||
alert(arr[0].name); // John
|
||||
alert(arr[1].name); // Mary
|
||||
alert(arr[2].name); // Pete
|
||||
```
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue