Q:

javascript ascending and descending

// 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]
6
// ascending and discending for number
const arr1 = [21, 2100, 2, 35000];
const arr2 = [21, 2100, 2, 35000];

let ascN = arr1.sort((f, s) => f - s);
let dscN = arr2.sort((f, s) => s - f);

// ascending and discending for string
const arr3 = ['21', '2100', '2', '35000'];
const arr4 = ['21', '2100', '2', '35000'];

let ascS = arr3.sort((f, s) => f.length - s.length);
let dscS = arr4.sort((f, s) => s.length - f.length);
0
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
1
function myFunction() {
  points.sort(function(a, b){return a-b});
  document.getElementById("demo").innerHTML = points;
}
0

New to Communities?

Join the community