sort an array based on the length of its values in acending in javascript code example
Example 1: sort array by string length javascript
function sortByLength(arr) {
return arr.sort((x,y) => x.length - y.length);
}
Example 2: sort js
numArray.sort((a, b) => a - b); // For ascending sort
numArray.sort((a, b) => b - a); // For descending sort