Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
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. –  FrustratedWithFormsDesigner May 2 '13 at 20:24
    
"sort the second list and merge it into the first list" - I assume you meant "append it to the first list"? –  Kilian Foth May 2 '13 at 20:37
    
@FrustratedWithFormsDesigner thanks for the feedback –  Pablo Vadear May 3 '13 at 1:01
    
@KilianFoth yes, thank you for the clarification –  Pablo Vadear May 3 '13 at 1:02
    
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. –  Emmad Kareem May 3 '13 at 9:05
show 1 more comment

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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! –  Pablo Vadear May 3 '13 at 1:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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