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
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
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);
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
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us