Example 1: sort javascript array
var points = [40, 100, 1, 5, 25, 10];
points.sort((a,b) => a-b)
Example 2: javascript sort
var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
return a - b;
});
console.log(numbers);
Example 3: array sort js
arr = ['width', 'score', done', 'neither' ]
arr.sort()
arr.sort((a,b) => a.localeCompare(b))
arr = [21, 7, 5.6, 102, 79]
arr.sort((a, b) => a - b)
Example 4: javascript sort
homes.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));
Example 5: sort js array array
var ages = [18, 21, 9, 41, 35, 24]
ages.sort(function(a, b) {
return a - b
})
Example 6: sort function
var vehicules= [
{ nom: "Peugeot 208 1,2l PureTech", img:"208.png" , co2:108 },
{ nom: "Peugeot 5008 2.0 BlueHDi 150", img:"5008.png" , co2: 118 },
{ nom: "Golf GTI", img:"golfgti.png" , co2:148 },
{ nom: "Renault Clio 0.9 TCe 90", img:"clio.jpg" , co2:114 },
{ nom: "Audi A4 2.0 TDI 190", img:"a4.png" , co2: 111},
{ nom: "BMW Serie 2 Tourer 220d", img:"serie2tourer.png" , co2:124 },
{ nom: "Audi SQ7", img:"sq7.png" , co2:198 },
{ nom: "Audi TTS", img:"tts.png" , co2:159 },
];
vehicules.sort(function (a, b) {
return a.co2 - b.co2;
});
vehicules.forEach(function(v) {
console.log(v.nom+" avec "+v.co2+" g de CO2/km");
});