I have constructed a program that takes an xml file parses the file and stores each case with it child nodes in a hashmap<string, string>. Now I must be able to sort each case by one of its child nodes (Priority) from high medium or low at the top in descending order. Then have the rest sorted by their timestamp also a child node of the case. Hopefully that makes sense any advice would be must appreciated!
Here is how a case looks:
Case Name : addrun
You can't sort a Map. It is not designed like that. You can only sort a List. It is easy enough to put all the objects into a List and sort that. I presume you are already familiar with sorting.
But there is something odd here. You say you are putting the objects into a Map<String, String>. That does not seem right; those objects are not Strings at all. A priority is not a String, but it might be an element of an enum. And what are child nodes? Do you mean that each object has a priority field?
I think you might do well to reconsider the entire design of the objects you are trying to sort. And please tell us why you are putting them into a Map: what does the “K” mean and what does the “V” mean?
Freddie van wrote:The only problem is the way to data is parsed is a string so there is no way around using it. Well as far as I can tell.
You receive the data as a String. That's fine. That doesn't mean you have to keep it as a String. As a first pass, you can define classes for each XML element. For instance, if you had something like this:
Then you should define at least a Person class and an Address class. You might want to just keep firstName, lastName, street, city, and state as Strings, but there are arguments for making them their own classes too. At the very least I'd make a State enum.
Then if the XML contains a bunch of person elements, you could create, for example a List<Person> in which to store them.
Freddie van der Neut
posted 5 years ago
Would it be possible to enum the priority element in the HashMap? If so what is the best way about doing that?