Answers for "what is sort in javascript"

3

sort js

numArray.sort((a, b) => a - b); // For ascending sort
numArray.sort((a, b) => b - a); // For descending sort
Posted by: Guest on October-20-2020
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
0

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
  }
})
Posted by: Guest on August-23-2021
-1

javascript sort

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
Posted by: Guest on February-09-2020
-1

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;
                               });
Posted by: Guest on June-25-2020
-2

how to sort array

array sorting
Posted by: Guest on February-28-2021

Browse Popular Code Answers by Language