forked from BmwDimasik/JSObjSort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (55 loc) · 1.27 KB
/
index.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const obj = [
{name: 'John', age: 21, weight: 309},
{name: 'Marie', age: 96, weight: 112},
{name: 'Alice', age: 32, weight: 245},
{name: 'Drake', age: 18, weight: 298},
{name: 'Josh', age: 76, weight: 35},
{name: 'Mark', age: 32, weight: 15},
{name: 'Joanna', age: 98, weight: 37},
{name: 'Mark', age: 13, weight: 405},
];
//sortByAge(obj);
//sortByName(obj);
sortByWeight(obj);
obj.forEach(obj => {
console.log("name: ", obj.name + ', age: ', obj.age + ", weight: ", obj.weight);
})
function sortByAge(obj) {
obj.sort(function(a, b) {
if (a.age == b.age) {
return a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1;
} else {
if (a.age > b.age) {
return 1;
} else {
return -1;
}
}
})
}
function sortByName(obj) {
obj.sort(function(a, b) {
if (a.name.toLowerCase() == b.name.toLowerCase()) {
return a.age > b.age ? 1 : -1;
} else {
if (a.name.toLowerCase() > b.name.toLowerCase()) {
return 1;
} else {
return -1;
}
}
})
}
function sortByWeight(obj) {
obj.sort(function(a, b) {
if (a.weight == b.weight) {
return a.age > b.age ? 1 : -1;
} else {
if (a.weight > b.weight) {
return 1;
} else {
return -1;
}
}
})
}