Month-Text Ordering
by David Wincelberg

Listing One

import java.util.Hashtable;

public class MonthOrder implements SortTest {
    Hashtable map = new Hashtable();

    public MonthOrder() {
        MonthInfo[] monthList = new MonthInfo[24];
        monthList[0]  = new MonthInfo ("JANUARY",   1);
        monthList[1]  = new MonthInfo ("FEBRUARY",  2);
        monthList[2]  = new MonthInfo ("MARCH",     3);
        monthList[3]  = new MonthInfo ("APRIL",     4);
        monthList[4]  = new MonthInfo ("MAY",       5);
        monthList[5]  = new MonthInfo ("JUNE",      6);
        monthList[6]  = new MonthInfo ("JULY",      7);
        monthList[7]  = new MonthInfo ("AUGUST",    8);
        monthList[8]  = new MonthInfo ("SEPTEMBER", 9);
        monthList[9]  = new MonthInfo ("OCTOBER",  10);
        monthList[10] = new MonthInfo ("NOVEMBER", 11);
        monthList[11] = new MonthInfo ("DECEMBER", 12);
        monthList[12] = new MonthInfo ("JAN",  1);
        monthList[13] = new MonthInfo ("FEB",  2);
        monthList[14] = new MonthInfo ("MAR",  3);
        monthList[15] = new MonthInfo ("APR",  4);
        monthList[16] = new MonthInfo ("JUN",  6);  // MAY is above
        monthList[17] = new MonthInfo ("JUL",  7);
        monthList[18] = new MonthInfo ("AUG",  8);
        monthList[19] = new MonthInfo ("SEP",  9);
        monthList[20] = new MonthInfo ("SEPT", 9);  // alternative
        monthList[21] = new MonthInfo ("OCT", 10);
        monthList[22] = new MonthInfo ("NOV", 11);
        monthList[23] = new MonthInfo ("DEC", 12);

        for (int i = 0;  i < monthList.length;  i++) {
            map.put (monthList[i].monthName, monthList[i]);
        }
    }   // MonthOrder
    private int findMonth (String monthName) {
        if (monthName == null) return -1;
        // Prepares test string
        String key = monthName.toUpperCase();
        int dot = key.indexOf ('.');    // trims off trailing dot
        if (dot != -1) key = key.substring (0, dot);

        MonthInfo mi = (MonthInfo) map.get (key);
        if (mi == null)
            return -1;
        else
            return mi.index;
    }   // findMonth

    ...
}


Listing Two

public int compareTo (String name1, String name2) {
    int m1 = findMonth (name1);
    int m2 = findMonth (name2);
    if (m1 == -1 && m2 == -1)       
        return simpleANCompareTo (name1, name2);
    else if (m1 == -1)      // month names appear before other items
        return +1;
    else if (m2 == -1)
        return -1;
    else if (m1 == m2)      // not needed, but makes order look nicer
        return name1.length() - name2.length();
    else
        return m1 - m2;
}   // compareTo


Listing Three

private int simpleANCompareTo (String name1, String name2) {
    int size1 = name1.length(), size2 = name2.length();
    int n1 = 0, n2 = 0, e1, e2;
    int val1, val2, test = 0;
    while (n1 < size1 && n2 < size2) {
        if (Character.isDigit (name1.charAt (n1))
        &&  Character.isDigit (name2.charAt (n2))) {
            // Finds ends of numbers so that parseInt will work
            for (e1 = n1 + 1;  e1 < size1
            && Character.isDigit (name1.charAt (e1));  )
                e1++;
            for (e2 = n2 + 1;  e2 < size2
            && Character.isDigit (name2.charAt (e2));  )
                e2++;
            try {
                val1 = Integer.parseInt (name1.substring (n1, e1));
            }
            catch (NumberFormatException ex) {
                val1 = -1;
            }
            try {
                val2 = Integer.parseInt (name2.substring (n2, e2));
            }
            catch (NumberFormatException ex) {
                val2 = -1;
            }
            if ((test = val1 - val2) != 0)
                return test;
            n1 = e1;
            n2 = e2;
        }
        else {  // caseless match
            test = Character.toLowerCase (name1.charAt (n1)) -
                   Character.toLowerCase (name2.charAt (n2));
            if (test != 0)
                return test;
            else {
                n1++;  n2++;

            }
        }
    }
    // One or more strings has ended
    if (n1 >= size1 && n2 >= size2)
        return 0;
    else if (n1 >= size1)
        return -1;
    else
        return +1;
}   // simpleANCompareTo


Listing Four

void removeButton_ActionPerformed (java.awt.event.ActionEvent event)
{
    try {
        int count = itemList.getItemCount();

        for (int i = 0;  i < count;  ) {
            if (itemList.isIndexSelected (i)) {
                itemList.delItem (i);
                count--;
            }
            else
                i++;
        }
    } catch (Exception e) {
    }
}   // removeButton_ActionPerformed





3


