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 sorting an ArrayList Alphabetically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 24183
    
  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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sorting an ArrayList Alphabetically
 
Similar Threads
Reading an ArrayList from JSP to Servlet
How can i make a class Immutable if it has an instance variable of java.lang.Object?
implement own ArrayList ?
Parsing a text file to an arraylist
Need help with using arraylist