Based on Salman's answer, I wrote a small function to encapsulate it:
function sortArrayByLength(arr, ascYN) { arr.sort(function (a, b) {
then just call him
sortArrayByLength( myArray, true );
Please note that, unfortunately, functions cannot be added to the Array prototype as described on this page .
In addition, he changed the array passed as a parameter and returns nothing. This will lead to duplication of the array and will not be large for large arrays. If anyone has a better idea, make a comment!
Nico Sep 24 '15 at 2:55 2015-09-24 14:55
source share