This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Cloud Application Architecture Patterns: Designing, Building, and Modernizing for the Cloud and have Kyle Brown, Bobby Woolf and Joseph Yodor on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Printing the items of an ArrayList alphabetically based on the field title of the items of the list.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyone..How can i print the items of an ArrayList alphabetically based on the field title of the items of the Arraylist??i have 12 objects in the list with different fields and i want to print the items based upon the title field.how can i do that??
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sort the ArrayList using the Collections.sort() static method. You'll need to write an implementation of java.util.Comparator that knows how to compare instances of your class; it can do so just by calling compareTo() on the "name" members. Then you can just iterate over the list and print it out.
 
Miltos Georgiou
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnxs for the reply.I am beginning to see the picture now.But the fact is that i am completely unfamiliar with the sort method and the comparator.Lets say that my Arraylist is called L.If i do Collections.sort(L) what that would do???i just want to print the items based on the title field.there are other fields too but none of them are important.just the title field.the comparator how it is used in such a situation???can you please show me a txt file that shows the comparator actual use???Sorry if am being very annoying.:-(
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at Object Ordering.

You do not have to implement the sort algorithm, all you need to do is provide the compare method that compares two objects.
 
reply
    Bookmark Topic Watch Topic
  • New Topic