This commit is contained in:
Ilya Kantor 2019-02-01 22:29:20 +03:00
parent 6a3ca84f3e
commit 55220cccf0
2 changed files with 6 additions and 6 deletions

View file

@ -1,6 +1,6 @@
```js run no-beautify
function sortByName(arr) {
arr.sort((a, b) => b.name > a.name ? 1 : -1);
arr.sort((a, b) => a.age > b.age ? 1 : -1);
}
let john = { name: "John", age: 25 };
@ -12,6 +12,6 @@ let arr = [ john, pete, mary ];
sortByName(arr);
// now sorted is: [john, mary, pete]
alert(arr[1].name); // Mary
alert(arr[0].name); // John
alert(arr[2].name); // Pete
```

View file

@ -4,7 +4,7 @@ importance: 5
# Sort objects
Write the function `sortByName(users)` that gets an array of objects with property `name` and sorts it.
Write the function `sortByName(users)` that gets an array of objects with the `age` property and sorts them by `age`.
For instance:
@ -18,6 +18,6 @@ let arr = [ john, pete, mary ];
sortByName(arr);
// now: [john, mary, pete]
alert(arr[1].name); // Mary
alert(arr[0].name); // Mary
alert(arr[2].name); // Pete
```