Photo by patricia serna on Unsplash
Code Wars - Sort Array By String Length
Breaking down code war challenges in a simple and understandable way.
The Problem Statement
Write a function that takes an array of strings as an argument and returns a sorted array containing the same strings, ordered from shortest to longest.
For example, if this array were passed as an argument:
["Telescopes", "Glasses", "Eyes", "Monocles"]
Your function would return the following array:
["Eyes", "Glasses", "Monocles", "Telescopes"]
All of the strings in the array passed to your function will be different lengths, so you will not have to decide how to order multiple strings of the same length.
Analysis -Breaking down the question
We have to sort the array based on the length of each item from shortest to longest.
For this, we can use the sort method to get the array in ascending order.
The Syntax for solving the problem
//Sort Method
//The basic sort method
array.sort()
//The sort method takes a function also as shown below. It compares two values at a time
//in an array and returns the elements in ascending or descending order
array.sort((a,b)=> a - b) //This is an Es6 arrow function.
//A normal function will look like this:
array.sort(sortNumbers)
function sortNumbers (a,b){
return a - b
}
//Both works the same. Feel free to use whatever you feel comfortable with. I'm gonna use the Es6 arrow function.
The Solution
Let’s create a function that takes an array as a parameter as shown below.
function sortByLength (array) {
};
Let’s use the sort method and see what happens
function sortByLength (array) {
return array.sort() //This method sorts the array in alphabetical order
};
sortByLength (["Telescopes", "Glasses", "Eyes", "Monocles"]) //['Eyes', 'Glasses', 'Monocles', 'Telescopes']
//If we see the problem statement, it is the answer we want because the length of the
//items and alphabetical order are the same for this array.
//if we change Eyes to Hi, it will change and will not give the desired result.
sortByLength (["Telescopes", "Glasses", "Hi", "Monocles"]) //['Glasses', 'Hi', 'Monocles', 'Telescopes']
//As per our requirement Hi should be first in the array. So we need to think other ways to deal with it
Since basic sort method didn’t work for us, Let’s try sort method with a function as shown below
function sortByLength (array) {
return array.sort((a,b)=> a - b)
};
sortByLength (["Telescopes", "Glasses", "Hi", "Monocles"]) //["Telescopes", "Glasses", "Hi", "Monocles"]
//This doesn't work because it compares two elements and returns the array in asecending order
//Here, it compares "Telescopes", and "Glasses" but cannot which is bigger or which is short
//because it cannot compare strings
Since it cannot compare strings, we can use length on a and b to sort correctly.
function sortByLength (array) {
return array.sort((a,b)=> a.length - b.length)
};
sortByLength (["Telescopes", "Glasses", "Hi", "Monocles"])//['Hi', 'Glasses', 'Monocles', 'Telescopes']
That is how we can get the sorted array
Final Words
Understanding the problem statement and knowing what to use is half the battle. Learn to understand the question before quickly diving into solving the challenge.
If you come this far and liked it, show some appreciation by liking, commenting, or sharing this with your network. Someone might find it helpful. Thanks See you on another article.