Learning notes-How to sort using the CompareTo method in the comparable interface

Source: Internet
Author: User
Tags comparable

Interface comparable<t>

This interface forces the overall ordering of the objects for each class that implements it. This sort is called the natural ordering of the class, and the compareTo method of the class is called its natural comparison method .

The list of objects (and arrays) that implement this interface can be Collections.sort automatically sorted by (and Arrays.sort ). An object that implements this interface can be used as a key in an ordered map or an element in an ordered collection without specifying a comparer.

Method Details CompareTo
CompareTo (T o)
compares the order of this object with the specified object.  If the object is less than, equal to, or greater than the specified object, it returns a negative integer, 0, or a positive integer, respectively.
Exercise 1. First define a student class with a name, class, and score of three parameters, and inherit the comparable interface.

public class Student implements comparable<student>{
private int sid; School Number
Private String sname; Name
private double score; Scores

2. The inherited interface leads to the own method, which defines the rules so that they can be sorted by fractions.

public int CompareTo (Student s) {
if (This.score>s.getscore ()) {
return 1; A positive integer is greater than
}else if (This.score<s.getscore ()) {
return-1;//negative integer is less than
}else{
return 0; 0 is equal to
}
}

3. Construct a parametric and non-parametric method and encapsulate it.
4. Next, create a test class, and create 3 student objects with three parameters respectively.

public class Testcom {
public static void Main (string[] args) {
Student s1=new Student (101, "Xiao Qiang", 89.5);
Student s2=new Student (102, "Big Strong", 56.5);
Student S3=new Student (103, "Xiao Fei", 90);

5. Add three student information to the collection.

List<student> slist=new arraylist<student> ();
Slist.add (S1);
Slist.add (S2);
Slist.add (S3);

6. We can print the score before sorting by the for loop output.

System.out.println ("-------------------before sorting");
for (Student s:slist) {
System.out.println (S.getscore ());
}


7. Use the Collections.sort () method to sort ascending order.

Collections.sort (slist);

The 8.for loop prints the score after the sorted output.

System.out.println ("-------------------after sorting");
for (Student s:slist) {
System.out.println (S.getscore ());

7. We can get the highest score by sorting.

System.out.println ("Up to" +slist.get (Slist.size ()-1). Getscore ());

8. The output from the console is:

-------------------before sorting
89.5
56.5
90.0
After sorting-------------------
56.5
89.5
90.0
The maximum is divided into 90.0

Summary: We can use the CompareTo method in the comparable interface to sort objects that could not otherwise be compared by some kind of self-condition.

---Part of the content is excerpted from api_1.6

Learning notes-How to sort using the CompareTo method in the comparable interface

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.