Answers for "what is .sort javascript"

25

JS array sort

numArray.sort((a, b) => a - b); // For ascending sort
numArray.sort((a, b) => b - a); // For descending sort
Posted by: Guest on May-08-2021
0

js sort

let numbers = [4, 10, 5, 1, 3];
numbers.sort((a, b) => a - b);
console.log(numbers);

// [1, 2, 3, 4, 5]
Posted by: Guest on May-10-2021
-2

js sort an array

// sort an array
// by drinks: lowest to highest
function sortDrinkByPrice(drinks) {
	return drinks.sort((a, b) => {
		return a.price - b.price;
	});
}

// parse the array as parameters within the function
console.log(sortDrinkByPrice([{name: "lemonade", price: 50},{name: "lime", price: 10}]));
Posted by: Guest on March-12-2021

Browse Popular Code Answers by Language