Answers for "sort by in javascript"

14

sorting array from highest to lowest javascript

// Sort an array of numbers 
let numbers = [5, 13, 1, 44, 32, 15, 500]

// Lowest to highest
let lowestToHighest = numbers.sort((a, b) => a - b);
//Output: [1,5,13,15,32,44,500]

//Highest to lowest
let highestToLowest = numbers.sort((a, b) => b-a);
//Output: [500,44,32,15,13,5,1]
Posted by: Guest on March-25-2020
6

array sort by alphabetical javascript

users.sort((a, b) => a.firstname.localeCompare(b.firstname))
Posted by: Guest on August-29-2020
0

JavaScript Sorting Arrays

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();        // Sorts the elements of fruits
Posted by: Guest on February-27-2021
-1

sort javascript

arr.sort()
arr.sort(fonctionComparaison)
Posted by: Guest on April-14-2021
-1

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");    
});
Posted by: Guest on July-29-2020

Browse Popular Code Answers by Language