

Javascript: Sort Array of Strings by Length – thisPointer.com
source link: https://thispointer.com/javascript-sort-array-of-strings-by-length/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

We often use sorting while working with arrays. This article demonstrates easy ways to sort an array of strings by their length using different methods and various example illustrations. The strings within the array will be sorted based on their length. A string with the lowest length will be placed first in the array, followed by the next string with a lower length than others.
Table of Contents:
Sort an Array of Strings: Implementing sort() with compare()
Javascript’s sort() method sorts all the array elements and returns the sorted array.
The compare() function, when passed as an argument in the sort() method, defines the sorting order.
Example:-
Sort the array of strings by their length in ascending order [“George”, “Rubie”, “Veronica”, “Ed”, “Will”]
Code:-
let arrayOfStrings = ["George", "Rubie", "Veronica", "Ed", "Will"]; console.log("Before Sorting: " + arrayOfStrings ); // sort your own way by comparing lengths of elements arrayOfStrings.sort((element1, element2) => { return element1.length - element2.length;}); console.log("After Sorting: " + arrayOfStrings);
Output:-
Before Sorting: George,Rubie,Veronica,Ed,Will After Sorting: Ed,Will,Rubie,George,Veronica
Explanation:-
- Here, we use the sort() with compare() method passed as an argument in the above code.
- Within the compare() function, we implement a sorting mechanism to sort the calling array based on the lengths of elements.
Sort an Array of Strings: writing custom function
In the below code, we will be implementing the Bubble Sort. Bubble sort is used to sort the elements such that adjacent elements are repeatedly swapped if they are in the wrong order. This algorithm needs one complete pass without any swap to verify if all the elements are placed in the right order
Example:-
Sort the array of strings by their length in ascending order [“George”, “Rubie”, “Veronica”, “Ed”, “Will”]
Code:-
function sortByLength(_arrayOfStrings){ //get the length of the array passed in the argument const length = _arrayOfStrings.length; var temp; var newLength = length-1; // create a sorted array var sortedArray =_arrayOfStrings; do { temp = false; for (var i=0; i < newLength; i++) { //traverse the array and swap elements repeatedly if they are in wrong order if (sortedArray[i].length > sortedArray[i+1].length) { var temp = sortedArray[i]; sortedArray[i] = sortedArray[i+1]; sortedArray[i+1] = temp; temp = true; } } newLength--; } while (temp); // return sorted array return sortedArray; } //usage of the function let arrayOfStrings = ["George", "Rubie", "Veronica", "Ed", "Will"]; console.log("Before Sorting: " + arrayOfStrings); console.log("After Sorting: " + sortByLength(arrayOfStrings));
Output:-
Before Sorting: George,Rubie,Veronica,Ed,Will After Sorting: Ed,Will,Rubie,George,Veronica
Explanation:-
- Here bubble sort algorithm is usedto compare and re-arrange elements in the array based on the length of array elements.
Read More:
I hope this article helped you sort an array of strings by their length in javascript. Good Luck !!!
Recommend
-
11
Different ways to sort an array of strings in Swift How to sort an array of strings There are two ways to sort in Swift, The one that mutates the original array and the one that don't. Both of them...
-
14
In JavaScript, the array.sort() method sorts the array. Let’s use it to sort some numbers: const numbers = [10, 5, 11]; numbers.sort(); // => [10, 11, 5] Hm… numbers.sort()
-
11
While working with arrays in javascript, often there is a requirement to check if an array is empty or not. This article will describe how to check if an array is empty or exists in javascript. Table of Contents:-...
-
10
Very common requirement developers encounter while working with javascript arrays is to sort an array of strings. This article demonstrates easy ways to sort array of strings alphabetically in ascending as well as descending order using vario...
-
10
Very common requirement developers encounter while working with javascript arrays is to sort an array of integers. By default, an array containing digits will be considered as strings while sorting. This article demonstrates easy ways to sort...
-
13
This article will discuss how to convert Pandas Dataframe to Numpy Array. Table of Contents A Dataframe is a data structure that stores the data in rows and columns. We can create a DataFrame using pandas.Data...
-
9
This article will discuss how to convert Numpy arrays to a Pandas DataFrame. Table of Contents A DataFrame is a data structure that will store the data in rows and columns. We can create a DataFrame using panda...
-
10
C Program to Sort an array of names or stringsSkip to content
-
8
What Does Setting the Length of a JavaScript Array Do? Jan 23, 2023 The most common pattern is setting the length of the array to 0. Setting array.length = 0
-
4
Java Sort Array of StringsSkip to content...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK