KKK琦 2010-04-02 20:17

# 如何对List集合中的数据进行排列

[size=x-large]请问：怎样对List集合中的数据进行排序啊？？ 请给与详解！！！

• 写回答

#### 5条回答默认 最新

• iteye_13460 2010-04-02 21:33
关注

[size=large][color=red]要对List排序,你要对List里装的这种类型的类实现排序接口(Comparable).
举个例子:

下面这个例子是对List进行排序.使用Collections.sort(List list);方法,此方法会调用MyObject的compareTo方法.所以在MyObject类定义里要实现compareTo方法.
[code="java"]public class ListSort {

``````/**
* main()
* 2010-4-2,下午09:25:57
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<MyObject> lists = new ArrayList<MyObject>();
MyObject obj1 = new MyObject("d");
MyObject obj2 = new MyObject("a");
Collections.sort(lists);
for (MyObject myObject : lists) {
System.out.println(myObject.getContent());
}
}
``````

}
class MyObject implements Comparable{
private String content;
public MyObject(String _content) {
this.content = _content;
}

``````public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public int compareTo(MyObject obj) {
// TODO Auto-generated method stub
if (null == obj) return 1;
else {
return this.content.compareTo(obj.content);
}

}
``````

}[/code]

下面是Collections.sort方法
[code="java"]public static > void sort(List list) {
Object[] a = list.toArray();
Arrays.sort(a);
ListIterator i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
i.set((T)a[j]);
}
}[/code]

实际上我们的MyObject类的方法compareTo是在Arrays.sort()中被调用的...
请看..
[code="java"]public static void sort(Object[] a) {
Object[] aux = (Object[])a.clone();
mergeSort(aux, a, 0, a.length, 0);
}[/code]
[code="java"] 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++];
}
}[/code][/color][/size]
``````

当然,这是一种实现排序的办法.还有一种是实现Comparator,实现这个接口,然后使用
Collections.sort(List list,Comparator c);这个方法来排序..

原理差不多..

希望对你有帮助..

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

#### 悬赏问题

• ¥15 三分类机器学习模型可视化分析
• ¥15 本地测试网站127.0.0.1 已拒绝连接，如何解决？(标签-ubuntu)
• ¥50 Qt在release捕获异常并跟踪堆栈(有Demo，跑一下环境再回答)
• ¥30 python，LLM 文本提炼
• ¥15 关于将inet引入的相关问题
• ¥15 关于一个倒计时的操作和显示设计
• ¥15 提问STK的问题，哪位航天领域的同学会啊
• ¥15 苹果系统的mac m1芯片的笔记本使用ce修改器使用不了
• ¥15 单相逆变的电压电流双闭环中进行低通滤波PID算法改进
• ¥15 关于#java#的问题，请各位专家解答！