Java Sorting – Custom Alphabetic Sorting of Array

javalistsortingtext processing

I have a requirement to read a text file with lines in tag=value format and then output the file with specific tags listed first and the rest sorted alphabetically. The incoming file is randomly sorted with the exception of the first line. The output needs the first two lines to always be the same. For example, given the following tags:

NAME
AGE
SSN
MARITAL_STATUS
NO_OF_DEPENDENTS

The input will always have NAME first and the remaining tags (there are literally hundreds) sorted randomly. The output needs to have SSN first and NAME second and the rest sorted alphabetically so I would end up with:

SSN
NAME
AGE
MARITAL_STATUS
NO_OF_DEPENDENTS  

Note: These are just sample tags. The actual file has 13 fields which need to be listed first and the remaining few hundred listed alphabetically.

I'm trying to figure out the best way to do the sorting. Right now my plan is to read the lines in the incoming file and marshal them into two List objects. The first will contain the specific tags which need to be placed first and the second will have everything else. I will then sort the second list and merge it into the first list. This seems complicated and I feel like I'm missing an easier or more elegant approach.

Best Answer

The data that is present most closely resembles a map of header to rest of value. This should point one in the direction of a Map rather than a List.

Of the data that is presented, there are two groupings of the data - the first 13 fields, and all the rest. The presentation of the all the rest is to be used in a sorted order. For this, one looks at the SortedMap interface and sees the TreeMap as one if its implementations.

The 13 field data can be used as a little known map type - the EnumMap.

With the EnumMap, one would first define the enumerations of the fields in the order desired.

public enum Headers {
    SSN,
    NAME;
}

One then gets the associated code that looks something like (lacking the looping over the data):

SortedMap<String, String> other = new TreeMap<String, String>();
EnumMap<Headers, String> headers = new EnumMap<Headers, String>(Headers.class);
try {
    headers.put(Headers.valueOf(key), value);
} catch (IllegalArgumentException  e) {
    other.put(key, value);
}

If the header is present in the enum, put it in the headers map, otherwise put it in the other map.

At this point, one can then iterate over the headers EnumMap and then the other TreeMap printing out the key and value pairs. Or present a new object that itself extends Iterable and makes an iterator that first walks the EnumMap and then the SortedMap.

The enum provides easy extensibility of the code (if you want to do validation checking, one can associate it with the enum. See the Java tutorial on Enum Types to see some more things one can do with it (associate a method with the enum itself, associate values (Pattern? an instance of a class that has an interface providing a boolean validate(String arg)? display formatting code?) to further extend it.

Related Topic