File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Printing the items of an ArrayList alphabetically based on the field title of the items of the list. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Printing the items of an ArrayList alphabetically based on the field title of the items of the list." Watch "Printing the items of an ArrayList alphabetically based on the field title of the items of the list." New topic
Author

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

Miltos Georgiou
Greenhorn

Joined: Apr 18, 2009
Posts: 2
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??
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

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.


[Jess in Action][AskingGoodQuestions]
Miltos Georgiou
Greenhorn

Joined: Apr 18, 2009
Posts: 2
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.:-(
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing the items of an ArrayList alphabetically based on the field title of the items of the list.
 
Similar Threads
Sorting Collections
Sorting
request scoped form
Pagination in ListView
equals() in subclasses