-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortExercises.js
40 lines (31 loc) · 1.08 KB
/
sortExercises.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function leastToGreatest(arr) {
return arr.sort((a, b) => a - b);
}
console.log(leastToGreatest([1, 3, 5, 2, 90, 20])); // [1, 2, 3, 5, 20, 90]
function greatestToLeast(arr) {
return arr.sort((a, b) => b - a);
}
console.log(greatestToLeast([1, 3, 5, 2, 90, 20])); // [90, 20, 5, 3, 2, 1]
function lengthSort(arr) {
return arr.sort((a, b) => a.length - b.length);
}
console.log(lengthSort(["dog", "wolf", "by", "family", "eaten"])); // ["by", "dog", "wolf", "eaten", "family"]
function alphabetical(arr) {
return arr.sort();
}
console.log(alphabetical(["dog", "wolf", "by", "family", "eaten"])); // ["by", "dog", "eaten", "family", "wolf"]
function byAge(arr) {
return arr.sort((a, b) => a.age - b.age);
}
console.log(
byAge([
{ name: "Quiet Samurai", age: 22 },
{ name: "Arrogant Ambassador", age: 100 },
{ name: "Misunderstood Observer", age: 2 },
{ name: "Unlucky Swami", age: 77 }
])
);
// => [ { name: 'Misunderstood Observer', age: 2 },
// { name: 'Quiet Samurai', age: 22 },
// { name: 'Unlucky Swami', age: 77 },
// { name: 'Arrogant Ambassador', age: 100 } ]