• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding Arranging the elements in ArrayList

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Implementing the Comparable interface and calling Collections.sort.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic