Answers for "js sort array"

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
20

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
1

js sort

strData = ["banana", "apple", "zebra", "jos"];
  numData = [1, 3, 2, 5, 6];
  objData = [
    { name: 'Alex', age: 20 },
    { name: 'Karlsson', age: 19 },
    { name: 'Noah', age: 21 },
  ];


  // sort str alfabeticly (a-z)
  strData.sort((a,b) => {
    return a === b ? 0 : a < b ? -1 : 1
  });
  console.log(strData)


  // sort Objdata uppifrån å ner (ålder 19-21)
  objData.sort((a,b) => a.age - b.age)
  console.log(objData);


  // Small number to large number
  numData.sort()
  console.log(numData);
  // Large number to small number
  numData.sort((a,b) => b-a)
  console.log(numData);
Posted by: Guest on October-11-2021
52

sort javascript array

var points = [40, 100, 1, 5, 25, 10];
points.sort((a,b) => a-b)
Posted by: Guest on June-07-2020
2

js sort array

var points = [40, 100, 1, 5, 25, 10];
points.sort((a,b) => a-b)
Posted by: Guest on May-08-2020
0

js sort array

// Functionless
sort()

// Arrow function
sort((firstEl, secondEl) => { ... } )

// Compare function
sort(compareFn)

// Inline compare function
sort(function compareFn(firstEl, secondEl) { ... })
Posted by: Guest on October-07-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language