• Post Reply Bookmark Topic Watch Topic
  • New Topic

here who will call compareTo()?  RSS feed

 
Ranch Hand
Posts: 94
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


here my doubt is who is calling compareTo() here? after some tries i get to know that it is called by sort(), is this all happening internally? and how come the value of i and obj in the compareTo() differs? some says it compares this object to specified object, then i can't see them in this program like in code form. Does the sort() is doing all these things like calling compateTo() passing an element in the list? and if so then if it passes an argument(here an element in the list) and some how it should call that method on this object. so all these things are happening internally rite? its totally confusing me.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kiran kumar reddy wrote:

Collections.sort(list);



Your understanding is correct. The above method sorts the objects. All objects passed as part of List should implement Comparable interface.
It internally uses Arrays.sort() method to sort objects present in List.
You can check it's source code for more details.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!