forked from jamesqquick/javascript-array-functions-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.js
More file actions
40 lines (39 loc) · 895 Bytes
/
sort.js
File metadata and controls
40 lines (39 loc) · 895 Bytes
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
const characters = [
{
name: 'Luke Skywalker',
height: 172,
mass: 77,
eye_color: 'blue',
gender: 'male',
},
{
name: 'Darth Vader',
height: 202,
mass: 136,
eye_color: 'yellow',
gender: 'male',
},
{
name: 'Leia Organa',
height: 150,
mass: 49,
eye_color: 'brown',
gender: 'female',
},
{
name: 'Anakin Skywalker',
height: 188,
mass: 84,
eye_color: 'blue',
gender: 'male',
},
];
//***SORT***
//1. Sort by mass
console.log(characters.sort((a,b) => a.mass-b.mass))
//2. Sort by height
console.log(characters.sort((a,b) => a.height-b.height))
//3. Sort by name
console.log(characters.sort((a,b) => a.name < b.name ? -1 : 1))
//4. Sort by gender
console.log(characters.sort((a,b) => a.gender<b.gender ? -1 : 1))