Introduction to the Java language Tutorial (17): Comparable and Comparator interface

Source: Internet
Author: User
Tags arrays comparable sort

In actual development, we often need to sort some data, which is often saved using arrays or collections. For sorting data in an array, the API provides an array of tool classes, java.util.Arrays, in which a large number of sort methods are overloaded to sort the array of various types. For example, you can sort an array of type object with the following methods:

public static void sort (object[] a)

Suppose there are class course, as follows:

Package com.csst.relation;
public class Course {
    private String title;
    private double price;
    Public Course (String title, double price) {
       super ();
       this.title = title;
       This.price = Price;
    }
    Public Course (String title) {
       super ();
       this.title = title;
    }
    Public Course () {
       super ();
    }
    Public String GetTitle () {return
       title;
    }
    public void Settitle (String title) {
       this.title = title;
    }
    Public double GetPrice () {return price
       ;
    }
    public void Setprice (double price) {
       This.price = Price;
    }
}

If you have an array of course types, you need to sort, according to the arrays class method, as long as you pass the array as an argument to the sort method. But as long as we think about it, we will find the problem. For course objects, the so-called order should have a sorting standard, for example, by Price row, or name row? The sorting criteria are determined to sort multiple course objects.

By carefully reading the detailed description of the Arrays.sort method, you will find that there is a requirement that objects sorted using this method must be of type comparable. Comparable is an interface that, as long as the course class implements this interface, overrides the method:

public int compareTo (Object o)

Assuming that we want the course array to be sorted by price, the course class implements the comparable interface, overwriting the CompareTo method:

public int CompareTo (Object arg0) {
       //TODO auto-generated method stub
       Course c= (Course) arg0;
       if (this.price>c.price) {return
           1;
       } else if (this.price>c.price) {
           return-1
       } else{return
           0;
       }
    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.