1const books = [
2 {id: 1, name: 'The Lord of the Rings'},
3 {id: 2, name: 'A Tale of Two Cities'},
4 {id: 3, name: 'Don Quixote'},
5 {id: 4, name: 'The Hobbit'}
6]
7
8books.sort((a,b) => (a.name > b.name) ? 1 : ((b.name > a.name) ? -1 : 0));
1grossaryList = {
2 'bread': 1,
3 'apple': 6,
4 'milk': 1,
5 'orange': 3,
6 'broccoli': 2
7}
8
9return Object
10 .entries(grossaryList)
11 .sort((a,b) => b[1]-a[1])
12
13//=> [['apple', 6],['orange', 3],['broccoli', 2],['bread',1],['milk', 1]]
1var maxSpeed = {
2 car: 300,
3 bike: 60,
4 motorbike: 200,
5 airplane: 1000,
6 helicopter: 400,
7 rocket: 28800
8};
9var sortable = [];
10for (var vehicle in maxSpeed) {
11 sortable.push([vehicle, maxSpeed[vehicle]]);
12}
13
14sortable.sort(function(a, b) {
15 return a[1] - b[1];
16});
17
18//[["bike", 60], ["motorbike", 200], ["car", 300],
19//["helicopter", 400], ["airplane", 1000], ["rocket", 28800]]