Comparable a custom ascending/descending sort of a class with a comparator

Source: Internet
Author: User
Tags comparable

1, basic rules

1.0 let your class implements comparable interface, override Method:int CompareTo (Object another)

1.1 Ascending obj1 > Obj2 return positive

Obj1 = = Obj2 return 0

Obj1 < OBJ2 return negative

1.2 Descending is the opposite of ascending order

1.3 If class already implements comparable interface, the comparison algorithm defined when sorted with the sort method of the collections class takes effect and can also be called: public static <T> void sort (list<t> List, comparator<? Super T> Comparator), passing a Comparator for custom sorting

2,comparable and Comparator differences

2.0 The former CompareTo has only one parameter, which allows the inner elements of the class to be compared with the parameters: int CompareTo (Object another)

The latter has two parameters, which are compared externally: int compare (t LHS, t RHS);

2.1 Position:

Comparable:java.lang

Comparator:java.util

Comparable a custom ascending/descending sort of a class with a comparator

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.