1var items = [
2 { name: 'Edward', value: 21 },
3 { name: 'Sharpe', value: 37 },
4 { name: 'And', value: 45 },
5 { name: 'The', value: -12 },
6 { name: 'Magnetic', value: 13 },
7 { name: 'Zeros', value: 37 }
8];
9
10// sort by value
11items.sort(function (a, b) {
12 return a.value - b.value;
13});
14
15// sort by name
16items.sort(function(a, b) {
17 var nameA = a.name.toUpperCase(); // ignore upper and lowercase
18 var nameB = b.name.toUpperCase(); // ignore upper and lowercase
19 if (nameA < nameB) {
20 return -1;
21 }
22 if (nameA > nameB) {
23 return 1;
24 }
25
26 // names must be equal
27 return 0;
28});
1itemsArray.sort(function(a, b){
2 return sortingArr.indexOf(a) - sortingArr.indexOf(b);
3});
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]]
1homes.sort(function(a, b) {
2 return parseFloat(a.price) - parseFloat(b.price);
3});
1var vehicules= [
2 { nom: "Peugeot 208 1,2l PureTech", img:"208.png" , co2:108 },
3 { nom: "Peugeot 5008 2.0 BlueHDi 150", img:"5008.png" , co2: 118 },
4 { nom: "Golf GTI", img:"golfgti.png" , co2:148 },
5 { nom: "Renault Clio 0.9 TCe 90", img:"clio.jpg" , co2:114 },
6 { nom: "Audi A4 2.0 TDI 190", img:"a4.png" , co2: 111},
7 { nom: "BMW Serie 2 Tourer 220d", img:"serie2tourer.png" , co2:124 },
8 { nom: "Audi SQ7", img:"sq7.png" , co2:198 },
9 { nom: "Audi TTS", img:"tts.png" , co2:159 },
10];
11vehicules.sort(function (a, b) {
12 return a.co2 - b.co2;
13});
14vehicules.forEach(function(v) {
15 console.log(v.nom+" avec "+v.co2+" g de CO2/km");
16});