my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Usage of Comparable for sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Usage of Comparable for sorting" Watch "Usage of Comparable for sorting" New topic
Author

Usage of Comparable for sorting

Sekhar Choudary
Ranch Hand

Joined: May 03, 2008
Posts: 57
can we use Comparable interface for sorting while using Arrays.sort() method
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
The comparable interface is used implicitely: If your class, say A, implements Comparable and you sort an array of type A[], its compareTo method is used for sorting.

Moreover, you can define a comparator class that implements the Comparator interface and use it explicitely in the sort method.


SCJP 5 (98%) - SCBCD 5 (98%)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Usage of Comparable for sorting
 
Similar Threads
Comparable And Comparator usage in real time
collections
collections: why duplicates arent getting eradicated?
why arrayLIst does not sort the elements i add
doubt in treeset...