guys i know that we have to implement the comparable interface if we want to use the sort() method generally....in any class.... but the point is i am not able to get how does the sort() and compareTo() methods work...i mean how sorting is getting done??? please help
For compareTo() go to the API documentation for the Comparable<T> interface, and it tells you what you have to implement.
For sorting, many methods use a version of recursive merge sort with omission of already-sorted subsets. Merge sort has the advantage of being stable (it doesn't swap round two objects with the same value) whereas the faster quicksort might swap them round. That means you can sort on name, then age, and the names will remain in order for the same age. For primitives, it doesn't matter whether the sorting is "stable" so they use quicksort.
You can Google for merge sort or sorting algorithms; this is a nice link with animations!
Joined: Sep 03, 2008
when we call sort() method then the compareTo() method is automatically getiing called?
The best way to learn about sorting is to write your own search routines. That way you will get first hand knowledge instead of trying to figure out what is going on in a black box.
Merge sort and quick sort are fairly complicated algorithms. Start simple, bubble sort is a quick and easy algorithm to implement. It should be very clear how compareTo gets used. It will also start you down the important path of time complexity.