1

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.

5
  • 2
    I'm not sure what's wrong with your approach - it's very similar to how I would have done it. I can think of alternate approaches, but they seem more complicated. May 2, 2013 at 20:24
  • "sort the second list and merge it into the first list" - I assume you meant "append it to the first list"? May 2, 2013 at 20:37
  • @FrustratedWithFormsDesigner thanks for the feedback May 3, 2013 at 1:01
  • A simple solution could be provided by using a dictionary to store tag names in the desired order. If you are interested, I could provide a C# implementation.
    – NoChance
    May 3, 2013 at 9:05
  • @Emmad Kareem Thanks for the feedback Emmad. There are two things which make that approach impractical for this project. First, there are hundreds of tags and second, the list is constantly changing. The only consistent items are the key fields. I've implemented Michael's solution below and it's actually quite elegant in practice. The two different map classes do all the sorting work for me quite nicely. Thanks again for the suggestion. May 3, 2013 at 23:26

1 Answer 1

1

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.

1
  • I had considered TreeMap but since I didn't need to do anything with the values I decided to just use the entire line rather than split it into key/value pairs. In hindsight it is more flexible doing it that way should I need to extend the functionality. Thanks for the feedback! May 3, 2013 at 1:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.