Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Usage of Comparable for sorting

 
Sekhar Choudary
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we use Comparable interface for sorting while using Arrays.sort() method
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic