aspose file tools*
The moose likes Java in General and the fly likes sorting an ArrayList Alphabetically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "sorting an ArrayList Alphabetically" Watch "sorting an ArrayList Alphabetically" New topic
Author

sorting an ArrayList Alphabetically

Shraddha Jain
Greenhorn

Joined: Feb 02, 2006
Posts: 27
i have done this by doing a toArray() on the linked list and then using compareto() for every element in the array, can this be done without using arrays? can i sort this ArrayList of strings only using the Collection class methods?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Did you consider using the static method sort() in the java.util.Collections class? Should do exactly what you want.


[Jess in Action][AskingGoodQuestions]
Shraddha Jain
Greenhorn

Joined: Feb 02, 2006
Posts: 27
oh yes! tried this now. but i see there's a method sort(list l ,Comparator c) can i specify how i want sort() to compare objects in the arraylist? using this method?
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Originally posted by Shraddha Jain:
oh yes! tried this now. but i see there's a method sort(list l ,Comparator c) can i specify how i want sort() to compare objects in the arraylist? using this method?


Collection.sort(List list)will sort in natural order.
sort(List list, Comparator c), The the comparator to determine the order of the list.All elements in the list must be mutually comparable.
 
Consider Paul's rocket mass heater.
 
subject: sorting an ArrayList Alphabetically