order an array of digits highest to lowest code example

Example 1: 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]

Example 2: javascript sort array of ints

function sortNumbers(a, b) {
  return a - b;
}
var ages = [12, 95, 43, 15, 10, 32, 65, 78, 8];
ages.sort(sortNumbers); //ages is now sorted low to high
//console.log(ages); [8, 10, 12, 15, 32, 43, 65, 78, 95]

Example 3: how to sort array least to greatest javascript stACK

function sortArray(array) {
  var temp = 0;
  for (var i = 0; i < array.length; i++) {
    for (var j = i; j < array.length; j++) {
      if (array[j] < array[i]) {
        temp = array[j];
        array[j] = array[i];
        array[i] = temp;
      }
    }
  }
  return array;
}

console.log(sortArray([3,1,2]));