30 May 2006

Month-Text Ordering

Reading Dr. Dobbs’s magazine December 2005 issue in the article Month-Text Ordering, appears the following phrase:

first we analyze QSortAlgorithm Class:

while( ( lo <> lo0 ) && (test.compareTo (a[hi], mid) > 0))

As we can see it’s unnecessary the amount of the number, only its sign, therefore it doesn’t affect at all the final result. thus we can improve the simpleANCompareTo method.

We can improved a little more taking out the IntParser method and return the difference of length or the compareTo method from String Class instead.

finally the new code in MonthOrder.java may be:


//Start New Code
if (e1!=e2) test = e1-e2;
else test = name1.substring (n1, e1).compareTo(name2.substring (n2, e2));

if (test != 0) return test; //End New Code

Diff file is:


87,99c89,94 < val1 =" Integer.parseInt" val1 =" -1;" val2 =" Integer.parseInt" val2 =" -1;" test =" val1"> > if (e1!=e2) test = e1-e2; > else test = name1.substring (n1, e1).compareTo(name2.substring (n2, e2)); > > > if (test != 0) 100a96 > 124a121,123 > > >


of course this new change is to avoid the limitation that integer range has (2^31-1), in this example it doesn’t matter the size because we are comparing years (yes, probably in the year 2147483647 this code will be obsolete :) ).

Perhaps it can be implemented something similar to the final version of numeric alphanumeric of October 2000 issue.

I have to appreciate David Wincelberg for sharing with us this simple and beautiful solution.

The original magazine’s source code can be obtained at:
ftp://66.77.27.238/sourcecode/ddj/2005/0512.zip

No comments :

My Blog List

Blog Archive

Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.