Answers for "descending order number in js"

25

ascending and descending val in array using js

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
3

javascript ascending and descending

// 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);
Posted by: Guest on June-21-2020
0

js reverse int in descending order

//Method 2
//rearrange the digits to create the highest possible number
function descendingOrder(n){
  let reversed = n.toString().split("").sort((a, b) => b - a).join('');
  return parseInt(reversed) * Math.sign(n);
}
console.log(descendingOrder(463));		// result:  643
console.log(descendingOrder(223744));	// result:  744322
Posted by: Guest on April-01-2021

Code answers related to "descending order number in js"

Browse Popular Code Answers by Language