Sorting numbers in arrays in javascript ES6

1 year ago23k times

Sorting numbers in arrays in Javascript ES6. If we want to use the javascript sortmethod with numerical values, we need to pass it inside a callback arrow function, which will handle the comparison of values.


const numbers = [1,5,3,2,19,8,4,18];
const sortnumbers = numbers.sort((a,b)=>a-b);
console.log(sortnumbers); // [1, 2, 3, 4, 5, 8, 18, 19]


Add Answer

Add a codeAdd Code
Remove adsremove

Latest codes

view all

Latest Snippets

view all