Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding sorting elements in ArrayList

 
yekkala krishna
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic