# 对象数组或list排序及Collections排序原理

1、对List<String>排序及Collections.sort的原理

        List<String> stringList = new ArrayList<String>();
stringList.add("nice");
stringList.add("delicious");
stringList.add("able");
stringList.add("moon");
stringList.add("try");
stringList.add("friend");

Collections.sort(stringList);

for (String str : stringList) {
System.out.println(str);
}

    @SuppressWarnings("unchecked")
public static <T extends Comparable<? super T>> void sort(List<T> list) {
Object[] array = list.toArray();
Arrays.sort(array);
int i = 0;
ListIterator<T> it = list.listIterator();
while (it.hasNext()) {
it.next();
it.set((T) array[i++]);
}
}

从中可以看出排序主体为Arrays.sort(array);Arrays的sort实现为

    public static void sort(Object[] array) {
// BEGIN android-changed
ComparableTimSort.sort(array);
// END android-changed
}

static void sort(Object[] a)到static void sort(Object[] a, int lo, int hi)到private static void binarySort(Object[] a, int lo, int hi, int start)。在binarySort中用于大小比较部分为

	Comparable<Object> pivot = (Comparable) a[start];
int left = lo;
int right = start;
assert left <= right;

while (left < right) {
int mid = (left + right) >>> 1;
if (pivot.compareTo(a[mid]) < 0)
right = mid;
else
left = mid + 1;
}

2、对自定义类进行比较

2.1 我们查看Object的实现发现其中并没有compareTo方法，

public final class Integer extends Number implements Comparable<Integer>

public final class String implements java.io.Serializable, Comparable<String>, CharSequence

我们可以发现他们都继承自Comparable

2.2 查看Comparable接口

public int compareTo(T o);

2.3 自定义类的比较

public class MainTest {

public static void main(String[] args) {
List<User> userList = new ArrayList<User>();
userList.add(new User("Lucy", 19));
userList.add(new User("Jack", 19));
userList.add(new User("Jim", 19));
userList.add(new User("James", 19));
userList.add(new User("Herry", 19));
userList.add(new User("Luccy", 19));
userList.add(new User("James", 18));
userList.add(new User("Herry", 20));

Collections.sort(userList);

for (User user : userList) {
System.out.println(user.getName() + "\t\t" + user.getAge());
}
}

private static class User implements Comparable<User> {

private String name;
private int    age;

public User(String name, int age){
this.name = name;
this.age = age;
}

@Override
public int compareTo(User another) {
int compareName = this.name.compareTo(another.getName());
if (compareName == 0) {
return (this.age == another.getAge() ? 0 : (this.age > another.getAge() ? 1 : -1));
}
return compareName;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}
}
}

Herry		19
Herry		20
Jack		19
James		18
James		19
Jim		19
Luccy		19
Lucy		19

a、继承自Comparable

private static class User implements Comparable<User>

b、实现compareTo方法

3、利用Collections sort的重载函数对自定义对象进行排序

public class MainTest {

public static void main(String[] args) {
List<User> userList = new ArrayList<User>();
userList.add(new User("Lucy", 19));
userList.add(new User("Jack", 19));
userList.add(new User("Jim", 19));
userList.add(new User("James", 19));
userList.add(new User("Herry", 19));
userList.add(new User("Luccy", 19));
userList.add(new User("James", 18));
userList.add(new User("Herry", 20));

Collections.sort(userList, new Comparator<User>() {

public int compare(User user1, User user2) {
int compareName = user1.getName().compareTo(user2.getName());
if (compareName == 0) {
return (user1.getAge() == user2.getAge() ? 0 : (user1.getAge() > user2.getAge() ? 1 : -1));
}
return compareName;
}
});

for (User user : userList) {
System.out.println(user.getName() + "\t\t" + user.getAge());
}
}

private static class User {

private String name;
private int    age;

public User(String name, int age){
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}
}
}

Collections.sort(userList, new Comparator<User>())

public static <T> void sort(List<T> list, Comparator<? super T> c)

public static <T> void sort(T[] a, Comparator<? super T> c)

private static void mergeSort(Object[] src, Object[] dest, int low, int high, int off, Comparator c)

	if (length < INSERTIONSORT_THRESHOLD) {
for (int i=low; i<high; i++)
for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)
swap(dest, j, j-1);
return;
}

调用Comparator的compare方法

4、以上两种排序性能的比较

binarySort需要进行nlg(n)次的比较最坏情况下n^2次的移动

mergeSort是不断进行二分，二分到很小部分后进行插入排序。所以会比较nlg(n)次移动nlg(n)次。但它需要先复制一份源数据，所以会多占用一倍的空间

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客