JS array sort
numArray.sort((a, b) => a - b); // For ascending sort
numArray.sort((a, b) => b - a); // For descending sort
JS array sort
numArray.sort((a, b) => a - b); // For ascending sort
numArray.sort((a, b) => b - a); // For descending sort
js sort
let numbers = [4, 10, 5, 1, 3];
numbers.sort((a, b) => a - b);
console.log(numbers);
// [1, 2, 3, 4, 5]
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}]));
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us