Phase 1 Language Basics + Advanced _1-3-Java language method _07 Collections Advanced _04- collection of tools _2_Collections collection of tools


Default rules are generally ascending order




again to create an array of strings

sorted in ascending order output

Custom Type Sort

Create a Person class, getter and setter structure and parameters have no argument constructor


method overrides the toString


pass an object into the compiler error. Sort of Person class, there are no rules.


Integer and String are implements the interface the Comparable

the CompareTo method to rewrite the interface, it is a sort of method. Only this way can rewrite sorted according to the rules.

String class is to rewrite the CompareTo method



Person implement the Comparable interface


We want to sort Person, renowned for generic write the Person class. Comparable <Person>

override method CompareTo



own rules define the comparison

result output is arranged in ascending order of age.


Age flashback arrangement




collation.
On the other hand is descending


 

Guess you like

Origin www.cnblogs.com/wangjunwei/p/11241297.html