Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Regarding sorting elements in ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regarding sorting elements in ArrayList" Watch "Regarding sorting elements in ArrayList" New topic
Author

Regarding sorting elements in ArrayList

yekkala krishna
Ranch Hand

Joined: Mar 04, 2007
Posts: 105
hi guys,

we use ArralyList classs to add elements with the help of Add() method.

Here in ArrayList Insertion order is preserved.

If i want to sort elements in the ASCENDING ORDER then what i have to do.

please suggest answer to this question.

with regards,
Y.Rama Krishna
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Use Collections.sort(List) or, if you want to improve the sort, use
Collections.sort(List, Comparator)

For further details, check the API

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html


Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Also look at the documentation for Set. See how it is similar or different from List. TreeSet will keep your items sorted all the time if the rest of the API is right for you.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding sorting elements in ArrayList
 
Similar Threads
List interface
ArrayList and Vector
why arrayLIst does not sort the elements i add
Removing items in an iterator
URGENT!!!