• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting ArrayList Values inside a TreeMap

 
Greenhorn
Posts: 5
jQuery Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to Javaranch Dinesh

well, how do you sort ArrayList<SampleVO> ?
 
Dinesh Kumar Durairaj
Greenhorn
Posts: 5
jQuery Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic