There are different algorithms for sorting an array of strings. Time-critical client applications often use the algorithm called Quicksort, which is very efficient for such applications. This two-part article series takes a close look at writing and using such a function.
You can sort an array of strings in ascending order (as in the dictionary) or in descending order (opposite to a dictionary's format). I will consider only the ascending order in this article.
Well, in an array of data (strings), it is possible to have two or more data items (strings) which are the same. So I will use the term “non-decreasing” instead of “ascending” to allow for data items that are the same.
I will first describe the algorithm. Normally, you sort an array of strings, not an array of letters from the alphabet. I use letters from the alphabet to do most of the explanation in this article. At the end of the article, I show how the function works with an array of strings.
After describing the algorithm, I explain how the computer can implement the algorithm. The implementation I give in this article is my own implementation. There are other implementations; I will not talk about those. I then write the function in JavaScript. I explain everything step by step.
We shall sort the following list before we consider strings:
Q, W, E, R, T, Y, U, I, O, P
The counting of array (list) elements is always done from position zero. So in the above list; Q is in position 0; W is in position 1; E is in position 2 and so on with P in position 9. There are 10 elements all together.