sort js
numArray.sort((a, b) => a - b); // For ascending sort
numArray.sort((a, b) => b - a); // For descending sort
sort js
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]
how to sort an array in javascript
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b}); // for ascending
//another way for ascending
points.sort((a,b)=>{
if(a>b){
return 1
}else if(a<b){
return -1
}else{
return 0
}
})
points.sort(function(a, b){return b - a}); // for descending
//another way for descending
points.sort((a,b)=>{
if(a>b){
return -1
}else if(a<b){
return 1
}else{
return 0
}
})
javascript sort
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
sort function explained javascript
var sortedArray = myArray.sort(function(a,b){
if (a.name < b.name)
return -1;
else if (a.name == b.name)
return 0;
else
return 1;
});
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