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 val in array using js

numArray.sort((a, b) => a - b); // For ascending sort
numArray.sort((a, b) => b - a); // For descending sort
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to return ascending array using for loop in js 
Javascript :: js enter key event listener 
Javascript :: guid generator node 
Javascript :: how to add button in canvas html5 
Javascript :: javascript dataurl to blob 
Javascript :: angular maxlength directive input type number 
Javascript :: how to get timestamp in javascript of a date object 
Javascript :: check if body has class javascript 
Javascript :: import bootstrap css and js file in react 
Javascript :: use regex to get urls from string 
Javascript :: moment to date 
Javascript :: powershell json = get value by key 
Javascript :: javascript switch 
Javascript :: clean react app 
Javascript :: eslint unexpected console statement 
Javascript :: js toggle boolean 
Javascript :: difference between devDependency and dependency 
Javascript :: Javscript Add days on Date 
Javascript :: async storage has been extracted from react-native core 
Javascript :: js cookies 
Javascript :: detect system dark mode javascrip 
Javascript :: how to iterate object inside object in javascript 
Javascript :: jquery click outside 
Javascript :: material ui textfield error 
Javascript :: moment day in range 
Javascript :: types of node in blockchain 
Javascript :: how to detect if ios is in dark mode react native 
Javascript :: js select element inside of div 
Javascript :: jquery image change on hover 
Javascript :: console.time in javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =