Answers for "order 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
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
0

JavaScript Sorting Arrays

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();        // Sorts the elements of fruits
Posted by: Guest on February-27-2021
-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
-3

mdn array sort

arr.sort([compareFunction])
Posted by: Guest on November-05-2020

Browse Popular Code Answers by Language