在java 中 对list排序用
//Collections.sort( userlist, comparator);
Collections.sort (userlist);//
User 类需要继承实现Comparable
对数组排序可直接用:
Arrays. sort(intArray );
package com.czy.sort;
public class User implements Comparable<User>
{
String name;
String age;
public User(String
name, String age) {
this. name =
name;
this. age =
age;
}
public String getAge()
{
return age ;
}
public void setAge(String
age) {
this. age =
age;
}
public String
getName() {
return name ;
}
public void setName(String
name) {
this. name =
name;
}
@Override
public int compareTo(User
user) {
int flag
= this.getAge().compareTo(user.getAge());
if (flag
== 0) {
return this .getName().compareTo(user.getName());
} else {
return flag;
}
}
}
package com.czy.sort;
import java.util.Comparator;
public class ComparatorUser implements Comparator{
public int compare(Object
arg0, Object arg1) {
User user0 = (User )
arg0;
User user1 = (User )
arg1;
// 首先比较年龄,如果年龄相同,则比较名字
int flag
= user0.getAge().compareTo(user1.getAge());
if (flag
== 0) {
returnuser0.getName().compareTo(user1.getName());
} else {
return flag;
}
}
}
package com.czy.sort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SortTest
{
public static void main(String[]
args) {
List userlist = new ArrayList();
userlist.add( new User( "dd", "4" ));
userlist.add( new User( "aa", "1" ));
userlist.add( new User( "ee", "5" ));
userlist.add( new User( "bb", "2" ));
userlist.add( new User( "ff", "5" ));
userlist.add( new User( "cc", "3" ));
userlist.add( new User( "gg", "6" ));
//ComparatorUser comparator = new ComparatorUser();
//Collections.sort( userlist,
comparator);
Collections.sort (userlist);//
User 类需要继承实现Comparable
for ( int i
= 0; i < userlist.size(); i++) {
User user_temp = (User) userlist.get(i);
System. out.println(user_temp.getAge()
+ "," + user_temp.getName());
}
}
}
在java 内部 还是将list 转换成array,然后再对数组进行排序
Object[] a = list.toArray();
Arrays. sort(a);
ListIterator<T> i = list.listIterator();
for ( int j=0;
j<a. length; j++) {
i.next();
i.set((T)a[j]);
}
再看 sort:
public static void sort(Object[]
a) {
Object[] aux = (Object[])a.clone();
mergeSort(aux, a, 0, a.length ,
0);
}
再看 mergeSort(归并排序):
private static void mergeSort (Object[]
src,
Object[] dest,
int low,
int high,
int off)
{
int length
= high - low;
// Insertion sort on smallest arrays
if (length
< INSERTIONSORT_THRESHOLD ) {
for ( int i=low;
i<high; i++)
for ( int j=i;
j>low &&
((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
swap(dest, j, j-1);
return ;
}
// Recursively sort halves of dest into src
int destLow
= low;
int destHigh
= high;
low += off;
high += off;
int mid
= (low + high) >>> 1;
mergeSort(dest, src, low, mid, -off);
mergeSort(dest, src, mid, high, -off);
// If list is already sorted, just copy from src
to dest. This is an
// optimization that results in faster sorts for
nearly ordered lists.
if (((Comparable)src[mid-1]).compareTo(src[mid])
<= 0) {
System. arraycopy(src, low, dest, destLow, length);
return ;
}
// Merge sorted halves (now in src) into dest
for( int i
= destLow, p = low, q = mid; i < destHigh; i++) {
if (q
>= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
dest[i] = src[p++];
else
dest[i] = src[q++];
}
}
本文详细介绍了在Java中使用Collections.sort()方法对List进行排序,并通过自定义Comparable接口实现个性化排序。同时,展示了如何对数组进行排序,以及内部实现的归并排序算法原理。此外,还探讨了将List转换为数组再进行排序的过程。
1万+

被折叠的 条评论
为什么被折叠?



