Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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
Comment

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);
Comment

js sort numbers descending order

// Sort Numbers in Descending Order
function sortDescending(num) {
	return Number(num.toString().split('').sort((a, b) => b - a).join(''));
//or: return parseInt(num.toString().split('').sort().reverse().join(''));
}

console.log(sortDescending(123)); // 321
console.log(sortDescending(1254859723)); // 9875543221
Comment

javascript sort numbers descending

var numArray = [140000, 32, 12, 63323, 104, 99];

numArray.sort(function(a, b) {
  return b - a;
});

// Array(6) [ 140000, 63323, 104, 99, 32, 12 ]
Comment

javascript sort Array descending order

//used arraow functions
const years = [1970, 1999, 1951, 1982, 1963, 2011, 2018, 1922]
const sortYears = arr =>{
  arr.sort((a,b)=>{return b-a})
  return arr
}
console.log(sortYears(years))
// Should print [ 2018, 2011, 1999, 1982, 1970, 1963, 1951, 1922 ]
Comment

PREVIOUS NEXT
Code Example
Javascript :: js select element inside of div 
Javascript :: javascript traverse 
Javascript :: bottom tab navigator react native transparent 
Javascript :: node eventemitter emit error 
Javascript :: play audio javascript 
Javascript :: How to Loop Through an Array with a for…in Loop in JavaScript 
Javascript :: sum of n numbers in javascript 
Javascript :: get the integer after decimal in javascript 
Javascript :: js remove special characters 
Javascript :: webpack set mode to development 
Javascript :: load +main.js with system.import 
Javascript :: pymongo add json to collection 
Javascript :: only allow numbers in text input in js 
Javascript :: comment in react 
Javascript :: javascript transitionduration 
Javascript :: javascript template string examples 
Javascript :: react onclick div 
Javascript :: how to send a message to a discord server using a bot 
Javascript :: destructuring dynamic properties 
Javascript :: how to detect which key is pressed in javascript 
Javascript :: javascript check if time is less than 
Javascript :: check if input is a number javascript 
Javascript :: convert date to millisecond in javascript 
Javascript :: javascript parse a json string 
Javascript :: zoom in canvas javascript 
Javascript :: loop through javascript array 
Javascript :: DataTypes Time sequelize 
Javascript :: how get value of json encode in laravel 
Javascript :: javascript store text file into string 
Javascript :: check undefined in javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =