Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to sort array numbers in ascending order in javascript

// how to sort array numbers in javascript without mutating original array.
const numbers = [100, 25, 1, 5];
const sorted = numbers.slice().sort((a, b) =>  a - b); // returns a new sorted array

console.log(numbers); // [100, 25, 1, 5]
console.log(sorted); // [1, 5, 25, 100]
Comment

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

PREVIOUS NEXT
Code Example
Javascript :: lodash empty string 
Javascript :: include cookies in fetch 
Javascript :: jquery append table row 
Javascript :: sequelize findone 
Javascript :: react conditionally disable button 
Javascript :: reduce array to object javascript 
Javascript :: onclick toggle class react 
Javascript :: split decimal value in javascript 
Javascript :: dynamic event listener jquery 
Javascript :: find and replace value in array of objects javascript 
Javascript :: javascript get all characters before a certain one 
Javascript :: check if string contains lowercase javascript 
Javascript :: js for loop array 
Javascript :: app.use 
Javascript :: Loop over all keys in the local storage 
Javascript :: stop interval js 
Javascript :: Cookies In NodeJS 
Javascript :: map javascript index 
Javascript :: javascript two character integer 
Javascript :: ajax onchange dropdown 
Javascript :: javascript cancel timeout 
Javascript :: count json objects 
Javascript :: if classlist contains js 
Javascript :: round js 
Javascript :: js var audio = new audio 
Javascript :: clear html element javascript 
Javascript :: bootstrap datepicker mindate and maxdate 
Javascript :: check if object has key lodash 
Javascript :: is a letter javascript 
Javascript :: js check if div have another div 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =