Comparable Interface

Objects that have an ordering are compared using the compareTo() method.

1. Comparable Interface

Objects of a class that implements the Comparable interface can be arranged into a particular order. For example, objects of class String, which implements Comparable, can be arranged into alphabetical order. Numeric classes such as Integer or Double implement Comparable and their objects can be arranged into numeric order.

Objects of a class that implements Comparable can be compared to each other to determine which comes first when the objects are arranged in order.

Chapter Topics:

      • The Comparable Interface
      • compareTo() method
      • natural order
      • collating sequence
      • sorting
      • binary search

Question 1:

Arrange these strings in order: "orange", "apple", "plum".

Source: Bradley Kjell,
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 License.