How to sort an array by the length of each element?

I have an array like this:

arr = [] arr[0] = "ab" arr[1] = "abcdefgh" arr[2] = "abcd" 

After sorting, the output array should be:

 arr[0] = "abcdefgh" arr[1] = "abcd" arr[2] = "ab" 

I mean, I want in decreasing order of length of each element.

+77
javascript string sorting arrays
May 17 '12 at 6:17
source share
6 answers

You can use the Array.sort method to sort the array. A sort function that considers the length of a string as a sort criterion can be used as follows:

 arr.sort(function(a, b){ // ASC -> a.length - b.length // DESC -> b.length - a.length return b.length - a.length; }); 



Note: sorting ["a", "b", "c"] by the length of the string does not guarantee the return of ["a", "b", "c"] . According to specifications :

Sorting is not necessarily stable (that is, elements that compare equal do not necessarily remain in their original order).

If the goal is sorting by length, then additional criteria must be specified in the order of the dictionary:

 ["c", "a", "b"].sort(function(a, b) { return a.length - b.length || // sort by length, if equal then a.localeCompare(b); // sort by dictionary order }); 
+198
May 17 '12 at 6:24
source share

Here is a sort, depending on the length of the string with javascript, as you requested:

[solving the problem by bubble sorting] [1]

[1]: http://jsfiddle.net/sssonline2/vcme3/2/ enter code here

+3
May 17 '12 at 6:29
source share

We can use the Array.sort method to sort this array.

ES5 Solution

 var array = ["ab", "abcdefgh", "abcd"]; array.sort(function(a, b){return b.length - a.length}); console.log(JSON.stringify(array, null, '\t')); 

For ascending sort order: a.length - b.length

For descending sort order: b.length - a.length

ES6 Solution

Warning: not all browsers can understand ES6 code!

In ES6, we can use arrow function expressions .

 let array = ["ab", "abcdefgh", "abcd"]; array.sort((a, b) => b.length - a.length); console.log(JSON.stringify(array, null, '\t')); 

+2
Jul 04 '18 at 19:35
source share

Based on Salman's answer, I wrote a small function to encapsulate it:

 function sortArrayByLength(arr, ascYN) { arr.sort(function (a, b) { // sort array by length of text if (ascYN) return a.length - b.length; // ASC -> a - b else return b.length - a.length; // DESC -> b - a }); } 

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!

0
Sep 24 '15 at
source share

I adapted @shareef's answer to make it concise. I use

.sort(function(arg1, arg2) { return arg1.length - arg2.length })

0
Aug 25 '16 at
source share
 <script> arr = [] arr[0] = "ab" arr[1] = "abcdefgh" arr[2] = "sdfds" arr.sort(function(a,b){ return a.length<b.length }) document.write(arr) </script> 

The anonymous function that you pass to the sort tells it how to sort the given array. Hope this helps. I know this is confusing, but you can tell the sorting functions how to sort the elements of the array by passing it a function as a parameter indicating what to do

-3
May 17 '12 at 6:44 AM
source share



All Articles