It's not a secret anymore!
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regarding sorting elements in ArrayList" Watch "Regarding sorting elements in ArrayList" New topic

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

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:
subject: Regarding sorting elements in ArrayList
It's not a secret anymore!