Sorting alphabets within a string in JavaScript


In the given problem statement we are required to sort the alphabets present in the string. And implement the solution with the help of Javascript functionalities.

Understanding the Problem

The problem we have is to create a function in Javascript with the help of it we can sort the given alphabets in the string in alphabetical order. The main objective is to take the string as input and produce a new string with the same characters but in a sorted form. For example is we have a string like "hello" so the sorted form of this string is "ehllo", which is in the alphabetical order.

Logic for the given Problem

To do this task we will use Javascript's predefined methods such as split, sort and join. The split method will allow us to split the given input string into the array of characters. The sort method will arrange the items of the array in the alphabetical order. And lastly the join method will combine the sorted characters back into a new sorted string. So basically all these methods will be inside a function to sort the alphabets. This function will take a string as a parameter and return the sorted string.

Algorithm

Step 1: As we have to sort the alphabets so for doing this task we will define a function and this function takes a parameter as string. This string acts as an input for which we have to sort the alphabets.

Step 2: In this function we will split the given string into an array of characters and store these splitted strings into chars variables.

Step 3: After splitting the string into an array we will sort these characters using the sort method of javascript. As this is the main task for this program.

Step 4: Now we have the sorted characters but we need a sorted string as per the given problem. So we will use the join method to join these characters of the array into a string.

Step 5: After joining the characters we will return the sorted string.

Example

function sortAlphabets(str) {
   // Split the string into an array of characters
   var chars = str.split('');
   // Sort the characters
   var sortedChars = chars.sort();

   var sortedStr = sortedChars.join('');

   return sortedStr;
}

var input = "tutorials point";
var sorted = sortAlphabets(input);
console.log(sorted);

Output

aiilnooprstttu

Complexity

The time complexity for sorting the alphabets within a string in Javascript with the help of the above function is O(n log n). In this n is the size of the input string. The reason for this complexity is that the function is using a sort method which is a comparison based sorting function and the function takes O(n log n) time to sort the given array. And the space required to the function is O(n) because the function needed to store the sorted characters in an array.

Conclusion

The function we have created sorts the alphabets of the provided string with the help of join, split and sort methods of Javascript. And the function has logarithmic time complexity with a constant space complexity.

Updated on: 16-Aug-2023

236 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements