aspose file tools*
The moose likes Java in General and the fly likes Sorting ArrayList Values inside a TreeMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting ArrayList Values inside a TreeMap" Watch "Sorting ArrayList Values inside a TreeMap" New topic
Author

Sorting ArrayList Values inside a TreeMap

Dinesh Kumar Durairaj
Greenhorn

Joined: Dec 10, 2008
Posts: 5

I have a TreeMap<String, ArrayList<SampleVO>>

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

Here is the VO



Start from scratch and keep on scratching..
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to Javaranch Dinesh

well, how do you sort ArrayList<SampleVO> ?
Dinesh Kumar Durairaj
Greenhorn

Joined: Dec 10, 2008
Posts: 5

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

Joined: Mar 07, 2007
Posts: 1282

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

Joined: Dec 10, 2008
Posts: 5

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting ArrayList Values inside a TreeMap