Sorting for dummies
-
Keep in mind this is using JDK 1.3, but even so. Fortunately, this method isn't actually used anymore. Someone later decided it might be easier to just implement a Comparator and use the Collections.sort() facility provided by the Java API. I think that same someone decided to leave the code around as a reminder that things can, in fact, get worse. Much worse.
The real kicker is that this method doesn't really do any sorting - there was another method that sorted each of the lists contained in the resulting hash map. I can only assume that this is yet another case of a contractor getting paid by the line.
public Collection sortStuff()
{Collection listA = new java.util.ArrayList();
Collection listB = new java.util.ArrayList();
Collection listC = new java.util.ArrayList();...
Collection listZ = new java.util.ArrayList();
Map sortedList = new HashMap();
Collection list = getCollectionOfStuff();
Iterator listit = list.iterator();
while (listit.hasNext())
{
ObjectInfo info = (ObjectInfo) listit.next();
String name = info.getName();
if (name.length() > 0)
{
String startLetter = name.substring(0, 1);
if ((startLetter.equals("A")) || (startLetter.equals("a")))
{
listA.add(info);
}
else if ((startLetter.equals("B")) || (startLetter.equals("b")))
{
listB.add(info);
}...
else if ((startLetter.equals("Z")) || (startLetter.equals("z")))
{
listZ.add(info);
}
}
}
sortedList.put("ListA", listA);
sortedList.put("ListB", listB);...
sortedList.put("ListZ", listZ);
setSortedStuff(sortedList);
}
-
Well, imagine how much worse this would be if it actually sorted the stuff instead of just grouping them by the first letter....
collection listAA = new List();
Collection listAB = new List();
//....
string startLetter = .....
string secondLetter = ....
string thirdLeter = ....
if(startLetter.equals("A") && secondLetter.equals("A") && thirdLetter.equals("A")....)
...
if(startLetter.equals("A") && secondLetter.equals("A") && thirdLetter.equals("B")........)
...
-
This routine vaguely resembles a contrived, incomplete and unnecessary implementation of the radix sort algorithm.
-
This routine vaguely resembles a contrived, incomplete and unnecessary implementation of the radix sort algorithm.
I concur.
http://en.wikipedia.org/wiki/Radix_sort