| 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
|
|
|
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.
|
 |
 |
|
|
subject: Printing the items of an ArrayList alphabetically based on the field title of the items of the list.
|
|
|