Alphabetical Sort

Author: Theodore Odeluga

This is a Node command line project and Node.js can be downloaded from here

Instructions for running a command line program with Node.js can be found here

The source files for this project can be downloaded from here

As we know, Sort is the easy to use standard JS utility for ordering arrays.

But what if just for practice, we wrote our own custom sort algorithm?

Without overthinking things, we could probably come up with a small, reusable design that simply works.

The implementation we’re going for here involves less than 30 lines of code (as seen from my text editor).

We just need a few prerequisites: 1) an empty array which will later contain our results, 2) an array containing a sample of unsorted characters and 3) an array containing the alphabet.

Let’s start with the alphabet.

Our sort function will use the alphabet array as a point of reference against which to make comparisons when we process the contents of unsorted.

While numbers are self-explanatory to a computer, the alphabet, a construct of human language, is relatively alien to our humble machine. It needs some guidance in this regard but this can be resolved with a simple numeric system to associate to alphabetical characters.

In addition to the two arrays, we’ll also need two variables – a variable to keep track of characters as they’re processed (we’ll call it count) and an iterator for the inner loop of a nested loop structure.

Let's declare those variables.

let i;
let count = -1;

You’ll notice count is initialized at -1. This is to make sure it begins at the right point to properly evaluate the contents of unsorted.

I noticed in early experimentation i was always out by 1 if it started at zero.

On this occasion, going back one step further than zero sets everything up for correct enumeration.

Next, we create our arrays.

let sorted = [];

let unsorted = ['z','f','u','x','b','n','e','m']; //Example

let alphabet =

['a','b','c','d',
'e','f','g','h',
'i','j','k','l',
'm','n','o','p',
'q','r','s','t',
'u','v','w','x',
'y','z'];

Sorted starts out as empty, as we’ll be using this to contain our finished results.

Unsorted is straightforward – just a random collection of unordered letters (you can experiment with different samples).

Finally, alphabet contains all the characters of the alphabet for us to use as an index reference.

After declaring all our variables and setting up the arrays, we can implement the sort function. Let’s examine it line by line.

function sortit() {
while(sorted.length != unsorted.length){
count++;
for(i=0; i < unsorted.length; i++){
if(alphabet[count] == unsorted[i]){
sorted.push(unsorted[i]);
}
}
}
}

sortit();
console.log(sorted);

The first line opens a while loop which checks for equality of lengths between the unsorted and sorted arrays.

The reason for this is as follows:

While sorted is not the same length as unsorted, this tells us there are still characters to sort.

Once the two arrays become equivalent in length after processing, the first part of the solution is complete.

This first requirement is simply to establish that all the unsorted characters have been processed.

The next step (even more crucial) is to confirm that the unsorted characters have been processed correctly.

We can do this because of the following.

JavaScript automatically creates and stores indexes of arrays as they are populated.

This means that the native indexOf() method can track the position of each element in any user defined array.

The count variable is essentially following indexOf() in the background. We don’t need to reference indexOf() directly in our code.

Therefore, while count increments, it can test its values against the index values of the alphabet array.

As a result, count will generate an equivalent value for each alphabetical element.

Through this it will attach the value to any equivalent element in unsorted and move that element from unsorted to sorted in correct order.

The i iterator will also capture the specific identity of each element in unsorted while count captures the numeric value of that character’s index in alphabet.

An association can then be made between each copy of a particular object in one array and its equivalent in the other.

As JS recognizes the equivalence between an array’s index value and the content of each array element at every value of the index, an equality test (or perhaps an equivalence test) is possible between count and i.

The final results are then pushed into the previously empty space of sorted and presented to the user via console.log.

Here’s the entire program for convenience.

let i;
let count = -1;
let sorted = [];

let unsorted = ['z','f','u','x','b','n','e','m'];

let alphabet =

['a','b','c','d',
'e','f','g','h',
'i','j','k','l',
'm','n','o','p',
'q','r','s','t',
'u','v','w','x',
'y','z'];

function sortit() {
while(sorted.length != unsorted.length){
count++;
for(i=0; i < unsorted.length; i++){
if(alphabet[count] == unsorted[i]){
sorted.push(unsorted[i]);
}
}
}
}

sortit();
console.log(sorted);

Further development

With a bit of forethought, this solution could be expanded to sort separate words not just letters (you could treat each word as an array in its own right, splitting up the characters contained for each word in turn). See what you can do with that suggestion and how the code presented here can help. In the meantime, thanks for reading and I hope all of the above has been useful. All the best.