sort by id ascending javascript code example

Example 1: javascript sort by id

elems.sort((a, b) => a.id - b.id);

Example 2: javascript sort function

var points = [40, 100, 1, 5, 25, 10];
points.sort((a,b) => a-b)

Example 3: javascript sort array in ascending order

//sorts arrays of numbers
function myFunction() {
  points.sort(function(a, b){return a-b});
  document.getElementById("demo").innerHTML = points;
}

Example 4: 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;
                               });

Tags:

Cpp Example