排序有两种,
基本数据类型的包装类Integer, Float, Double,Long,Byte等都实现的Comparable接口,用于列表List或数组arrays的排序
Comparable接口方法的实现,对象列表的升序降序接口
我们通过重写改接口方法,可以对列表进行升序或降序排列。
public int compareTo(T o);
This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class‘s natural ordering, and the class‘s compareTo method is referred to as its natural comparison method.
此接口对实现它的每个类的对象强加一个默认排序。这种排序被称为类的自然排序,类的compareTo方法被称为其自然比较方法。
Lists (and arrays) of objects that implement this interface can be sorted automatically by
只有实现的这个接口的对象list列表或array数组才可以使用sort方法让列表或数组的元素被自动排序
只需要实现compareTo()方法即可
public int compareTo(){}这个比较方法,,
如果要将对象列表进行升序排序,则第i个元素和第i+1元素 要满足a[i]>a[i+1] 返回1 a[i]
如果要将对象列表进行降序排序 要满足 a[i]>a[i+1] 返回-1 a[i]