Sorting with long numbers in javascript ES6

2 years ago23k times

Sorting with long numbers in Javascript ES6. If the numbers are bigger than 9 - we can use regular expressions to find them and sort elements of the array based on their values.


const r = /\d+/; 
const alphanumbers = ['items 12','items 11','items 1302','items 999','items 144'];
const sortnumbers = alphanumbers.sort((a,b) => {
     return a.match(r) - b.match(r);
});
console.log(sortnumbers); // ['items 11', 'items 12', 'items 144', 'items 999', 'items 1302']




Add Answer

Add a codeAdd Code
Remove adsremove

Latest codes

view all

Latest Snippets

view all