Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting HashMap  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
[PRIORITY] LOW
[STATUS] PASSED
[TIMESTAMP] 13674148
[MH_TEST] PAR
 
Marshal
Posts: 59409
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

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?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

You can't sort a Map. It is not designed like that.



Except for, e.g., java.util.SortedMap.
 
Campbell Ritchie
Marshal
Posts: 59409
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don’t think you sort a SortedMap; it does the sorting for itself.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Freddie van wrote: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>.


Campbell's absolutely right. Your XML file contains data, and data are best represented as objects in Java.

You might find the StringsAreBad page worth reading.

Winston
 
Freddie van der Neut
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Its the getTextContext function that returns everything in a string and the only way I have figured out how to get the values from the xml file without having to declare each instance.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would it be possible to enum the priority element in the HashMap? If so what is the best way about doing that?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Freddie van der Neut wrote:Would it be possible to enum the priority element in the HashMap?



It's possible, and probably advisable, to represent the priority as an enum, yes. I don't know what you mean by "enum the priority element in the Hashmap" though.
 
Freddie van der Neut
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well for instance how would you enumerate state is the example you provided?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Freddie van der Neut wrote:Well for instance how would you enumerate state is the example you provided?


Rather than firing questions which sound like you simply want us to supply you with the code, why not have a look at the tutorials and see if you can't work it out for yourself?

If you run into trouble, come back with what you tried.
People will be much happier to help if you ShowSomeEffort (←click).

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Freddie van der Neut wrote:Well for instance how would you enumerate state is the example you provided?



With an enum(⇐click).
 
You've gotta fight it! Don't give in! Read this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!