• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding Arranging the elements in ArrayList  RSS feed

 
Hareendranath Babu Kotha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can Any one help me , whether is ther any procedure/way to arrange elements of an ArrayList in Sorted Order.

Thanks in Advance,
Hari.K.

"True Friend Will Never Hurt"
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Implementing the Comparable interface and calling Collections.sort.
 
jayanandan thiagarajan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai
you can use Comparator class.It is very useful and interesting working on it.
i created one class by implementing this.we can sort the objects in it by the property of the object also.So try it.you should use reflection package for getting method to access the property value.Very easy and interested one.
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi.. there is no need to implement comparable interface.. you can do it as below

List l = new ArrayList();
l.add(new Integer(10));
l.add(new Integer(5));
l.add(new Integer(15));
l.add(new Integer(1));

System.out.println(l);
Collections.sort(l);
System.out.println(l);
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is no need to implement comparable interface..

Wrong
Integer already implements it, that's why you don't need to.
Hareendranath never talks about Integers in the ArrayList. If you've got Person, Car or other kind of objects, how do you sort them ? By implementing the Comparable interface, of as Jayanandan said, by using a Comparator.

1. Collections.sort(List list)
-> in this case All elements in the list must implement the Comparable interface. (quoting the API)

2. Collections.sort(List list, Comparator c)
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well. ofcourse you are wright. But in my case/example that is valid.
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of the time we tend to use the String objects or Wrapper objects as elements of Collection classes.. So I meant in that terms.. You are absolutely right in saying that for own classes you need to implement Comparable interface. I Agree
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chetan Raju:
Most of the time we tend to use the String objects or Wrapper objects as elements of Collection classes..


Perhaps this is true in SCJP questions, but otherwise, not so much.

If this stimulating discussion needs to be continued, it can be continued in Java in General (Beginner).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!