When to Use the Spaceship Operator in Groovy

The spaceship operator, i.e., <=>, is useful in implementing a compare method in Groovy to indicate how objects should be sorted. Let's consider an example. We have a Person class that contains last name, first name and email. We would like to store persons in an array in an arbitrary order and then sort the array in last name sequence. This task requires that we inform the sort function how to sort the objects. To meet this requirement, we implement the Comparable interface in the Person class and provide a compare method that uses the spaceship operator to compare last names.

The input argument is the last name taken from an existing array element of type Person. The last name stored in the current object will be compared to the argument. If the operator returns a -1, then the instance last name is less than the argument. A return of 0 indicates that the names are equal and a value of +1 means the instance last name is greater than the argument. In this way, we use the spaceship operator when we need to tell Groovy where to place an object into an array.

In this topic you will implement the scenario presented above. To learn when to use the spaceship operator in Groovy follow these 3 steps:

  1. Open your text editor and type in the following lines of Groovy code:
    class Person implements Comparable{
    	String lastName
    	String firstName
    	String emailAddress
    	public String toString() {
    		"Name: $lastName, $firstName Email: $emailAddress"
    	int compareTo(personPassed) {
    def stephen = new Person(firstName: "Stephen", lastName: "Withrow", emailAddress: "sfw@SmoothMail.com")
    def bernard = new Person(firstName: "Bernard", lastName: "Fogelstein", emailAddress: "bernard@OtherGuys.com")
    def regina = new Person(firstName: "Regina", lastName: "Roberts", emailAddress: "rr@CoolMail.com")
    def nancy = new Person(firstName: "Nancy", lastName: "Abe", emailAddress: "NAbe@ReliableRulers.com")
    def william = new Person(firstName: "William", lastName: "Lee", emailAddress: "WilliamLee@CoolMail.com")
    def persons = [stephen, bernard, regina, nancy, william]
    println("Unsorted array:")
    for (p in persons) {
    println("Sorted array:")
    for (p in persons.sort()) {
    The script defines the Person class and contains the code to test the class. Note that the class implements Comparable and defines the compare method as described earlier. An array is created with persons stored in an arbitrary order. The array is printed to the console. Next, the sorted array is printed. At run time, the output will reveal that the array has been sequenced in last name order.
  2. Save your file as SpaceshipOperator.groovy.
  3. In the command prompt, type in the command to interpret and run your script:
    Run Spaceship operator script
    The output displays the unsorted persons first, followed by the persons sorted in last name sequence as the result of applying the spaceship operator.
Author: Stephen Withrow

Stephen has over 30 years of experience in training, development, and consulting in a variety of technology areas including Python, Java, C, C++, XML, JavaScript, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen has a degree in Computer Science and Physics from Florida State University.

About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.