-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-sort.html
78 lines (73 loc) · 2.19 KB
/
01-sort.html
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Books</title>
</head>
<body>
<table>
<thead>
<tr>
<th>
<a class="sort header" id="sort" href="#">Friend</a>
</th>
<td class="header">Books</td>
</tr>
</thead>
<tbody id="friends">
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
var $tbody = $('#friends');
var $sortName = $('#sort');
var friends = [
{name: 'Oliver', books: ['Ansible for DevOps', 'Servers for hackers']},
{name: 'Barry', books: ['Working effectively with unit tests', '50 quick ideas for your tests']},
{name: 'Jessica', books: ['Understanding the 4 rules of simple design', 'Principles of package design']},
{name: 'Clark', books: ['Selling test driven projects']}
];
var buildTableRows = function (friends) {
var table = '';
friends.forEach(function (friend) {
table += '<tr><td>' + friend.name + '</td><td>' + friend.books.join(', ') + '</td></tr>';
});
return table;
}
var sortFriendsByName = function(friends) {
// var friendArray = [];
//
// friends.forEach(function(friend){
// friendArray.push(friend.name);
// });
//
// friendArray.sort();
// function (friends) {
// var table = '';
// friends.forEach(function (friend) {
// table += '<tr><td>' + friendArray[friend] + '</td><td>' + friend.books.join(', ') + '</td></tr>';
// });
// }
// return table;
// }
// console.log(friendArray);
friends.sort(function (friendA, friendB) {
if (friendA.name < friendB.name) {
return -1;
}
if (friendA.name > friendB.name) {
return 1;
}
return 0;
});
};
var sortTable = function (event) {
event.preventDefault();
sortFriendsByName(friends);
$tbody.html(buildTableRows(friends));
}
$tbody.html(buildTableRows(friends));
$sortName.click(sortTable)
</script>
</body>
</html>