File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting ArrayList Values inside a TreeMap

 
Dinesh Kumar Durairaj
Greenhorn
Posts: 5
Java jQuery Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a TreeMap<String, ArrayList<SampleVO>>

i need to sort by attributes in VO. How do i do that?

Here is the VO

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Javaranch Dinesh

well, how do you sort ArrayList<SampleVO> ?
 
Dinesh Kumar Durairaj
Greenhorn
Posts: 5
Java jQuery Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for Welcoming me
seetharaman venkatasamy wrote:Welcome to Javaranch Dinesh

well, how do you sort ArrayList<SampleVO> ?


I need to sort the VOs in the ArrayList by some attribute of VO like id or name. or is it not possible ? :|

If input is
mapkey1,arrlistvo1->arrlistvo1(somevo1,somevo2...)->somevo1("xyz",1),somevo2("abc",2)

The result of sorting VO by name should be

mapkey1,arrlistvo1->arrlistvo1(somevo2,somevo1...)->somevo2("abc",2),somevo1("xyz",1)

The result of sorting VO by id should be

mapkey1,arrlistvo1->arrlistvo1(somevo1,somevo2...)->somevo1("xyz",1),somevo2("abc",2)

 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dinesh,

the general solution to your original problem is that you would define a Comparator for the TreeMap which is used to compare any two elements (=ArrayList<SampleVO>) of the TreeMap and decides which one should be before or after the other. But nevertheless YOU as a developer have to define what makes your ArrayLists comparable and defines the order when compared with another ArrayList.

Marco
 
Dinesh Kumar Durairaj
Greenhorn
Posts: 5
Java jQuery Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response, Marco

I implemented Comparator interface on the VO and override compareTo method based on your suggestion.

TreeMap values were sorted based on the keyname.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic