Thursday, March 13, 2014

The Order of Things

One of the great things about computers and software is that it can very quickly and accurately alphabetize lists.  This sort of task is perfect for a processor as it can order a list of 100 names if literally the blink of an eye.  Contrast the same task to a human which will take in the order of numerous minutes to accomplish.  However, one thing that computers don’t deal well with is exceptions to rules.  I was recently setting up a computer and I had setup numerous login IDs and the problem that I faced was that the name of the primary user of the computer was in the middle of the alphabet and I wanted this user listed first as she would be the main user of the computer. 
In order to override how a computer sorts, it’s important to understand how sorting is done in the first place.  Each character on the keyboards is assigned a numeric value – and the sorting is then done by comparing two characters to see which has the lower value.  The numbers 0-9 have a lower sequence than the letters A-Z followed by a-z.  The trick is in understanding that the <space> bar generates a character value just as any displayed character does and as such the name “John Smith” actually contains 10 characters (4 for the first name a space and then 5 for the last name).  Using this knowledge, a SPACE character can be added to the beginning of a name to force it towards the top of the list. 
To illustrate, let’s consider the following list of people without any special prefix and the resulting sort order:
Bob
Jane
John
Peter
Sam

If John was to be the primary user of the computer – and the name that is desired to be at the top of the list, then adding a space to the front of his name will result in the reordered list

 John
Bob
Jane
Peter
Sam

One caveat to the above approach, sometimes software or websites will remove all leading or trailing spaces, so this technique won’t work.  The alternative in this case is to use some other character other than the space.

- John
Bob
Jane
Peter

Sam

No comments:

Post a Comment