fix
This commit is contained in:
parent
6a3ca84f3e
commit
55220cccf0
2 changed files with 6 additions and 6 deletions
|
@ -1,6 +1,6 @@
|
||||||
```js run no-beautify
|
```js run no-beautify
|
||||||
function sortByName(arr) {
|
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 };
|
let john = { name: "John", age: 25 };
|
||||||
|
@ -12,6 +12,6 @@ let arr = [ john, pete, mary ];
|
||||||
sortByName(arr);
|
sortByName(arr);
|
||||||
|
|
||||||
// now sorted is: [john, mary, pete]
|
// now sorted is: [john, mary, pete]
|
||||||
alert(arr[1].name); // Mary
|
alert(arr[0].name); // John
|
||||||
|
alert(arr[2].name); // Pete
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ importance: 5
|
||||||
|
|
||||||
# Sort objects
|
# 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:
|
For instance:
|
||||||
|
|
||||||
|
@ -18,6 +18,6 @@ let arr = [ john, pete, mary ];
|
||||||
sortByName(arr);
|
sortByName(arr);
|
||||||
|
|
||||||
// now: [john, mary, pete]
|
// now: [john, mary, pete]
|
||||||
alert(arr[1].name); // Mary
|
alert(arr[0].name); // Mary
|
||||||
|
alert(arr[2].name); // Pete
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue