Forums Register Login

Sorting ArrayList Values inside a TreeMap

+Pie Number of slices to send: Send
I have a TreeMap<String, ArrayList<SampleVO>>

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

Here is the VO

+Pie Number of slices to send: Send
Welcome to Javaranch Dinesh

well, how do you sort ArrayList<SampleVO> ?
+Pie Number of slices to send: Send
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)

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2972 times.
Similar Threads
Regarding xml Data
List list = new ArrayList()
List box initialization through managed bean
Compiler warning for TreeMap
Why will TreeMap<K,V>(Comparator) not compile?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 20:20:43.