Saturday, December 29, 2012

Intuitive Sort Order for Alphanumeric Strings

I have developed some code to sort arrays of strings which contain a mixture of letters and numbers.  A common problem in string sorts is that the natural alphabetical order is not the same as the order of digits, since we consider consecutive digits in the aggregate but consecutive letters individually.  Hence, in alphabetical order "str10" precedes "str2", because 1 is before 2 alphabetically.  What we normally want to do is to compare 10 with 2, which would cause us to say "str2" precedes "str10".

To accomplish this I first create linked lists of alphameric and numeric strings (one linked list for each string).  I then compare the corresponding nodes of the two lists.  Permission is granted to use and adapt this code on an "as is" basis with no warranty of any kind.

Post a Comment