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

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

谢谢[/size]

5个回答

[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");
    lists.add(obj1);
    lists.add(obj2);
    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);这个方法来排序..

原理差不多..

希望对你有帮助..

冒泡可以啊!
for (int i = 1; i < list.size; i++) {
String temp = (String)list.get(i);
for (int j = 0; j < i; j++) {
if(compareTo(list.get(j).)>0){
String temp = sourceAry[i];
sourceAry[i]=sourceAry[j];
sourceAry[j]=temp;
}
}
}

实现一个自定义Comparator,然后使用Collections.sort方法

冒泡可以!我这里是对数组做排序,你改成list就可以了。
String[] sourceAry = {"1","6","4","8","3","9"};
for (int i = 1; i < sourceAry.length; i++) {
for (int j = 0; j < i; j++) {
if(sourceAry[i].compareTo(sourceAry[j])>0){
String temp = sourceAry[i];
sourceAry[i]=sourceAry[j];
sourceAry[j]=temp;
}
}
}

java.util.Collections类的sort方法也可以实现。
public static class Comparator1 implements Comparator {

/**
 * 功能:对单位进行比较
 * 
 * @return  1, o1在o2之后
 *         -1, o1在o2之前
 */
public int compare(Object o1, Object o2) {
  String[] ary1 = (String[]) o1;
  String[] ary2 = (String[]) o2;

  if (ary1[3].equals(ary2[3])) { //  1. 是同级别
    if (ary1[8].equals("") && ary2[8].equals("")) {
      return 0;
    } else if (ary1[8].equals("")) {
      return 1;
    } else if (ary2[8].equals("")) {
      return -1;
    } else {
        return Integer.parseInt(ary1[8])-Integer.parseInt(ary2[8]);
    }
  } else { //  2. 不是同级别
    if (ary1[3].equals("") && ary2[3].equals("")) {
      return 0;
    } else if (ary1[3].equals("")) {
      return 1;
    } else if (ary2[3].equals("")) {
      return -1;
    } else {
        return ary1[3].compareTo(ary2[3]);
    }
  }
}

}

Collections.sort(dwCodeList, new Comparator1());

Comparator cmp = new Comparator(){
@Override
public int compare(String o1, String o2) {
int len1 = o1.length();
int len2 = o2.length();
int len = Math.min(len1, len2);
for(int i=0;i if(o2.charAt(i) > o1.charAt(i)){
return 1;
}
else if(o2.charAt(i) < o1.charAt(i)){
return -1;
}
}
//if之前的都相等,看谁更长一些
if(len2 > len1){
return 1;
}
else if(len2 < len1){
return -1;
}
else{
return 0;
}
}

    };

你要再不会自己思考,那就真的太懒了。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问