在Java中对数组列表进行排序

3
我有一个类,其中包含另一个名为Iris的类,具有一些属性。
public class Helper {

    Iris iris;
    double distance;

    public Helper(Iris iris, double distance) {
        this.iris = iris;
        this.distance = distance;
    }
}

我想对这个数组列表(即List < Helper > helperList)按照距离参数进行降序排序。我编写了以下方法,但它没有起作用。
public void sort(){
for(int k=0; k < helperList.size(); k++)
        {
            double distance = helperList.get(k).distance; 

             for(int l=0; l < helperList.size(); l++)
             {
                 Helper temp = helperList.get(l);
                 if( distance < temp.distance )
                 {
                     helperList.set(l, helperList.get(k));
                     helperList.set(k, temp);
                 }
            }
        }
}

有人能提供一个解决方案吗?


它的哪个方面出了问题? - Michael Petrotta
它没有正确地对列表进行排序。 - Ammar
1
考虑两个元素的情况,比如 [1, 2]。当 k = 0, l = 1 时,因为 list.get(1) > 1,所以你进行了交换,得到 [2, 1]。然后当 k = 1, l = 0 时,你再次进行交换。你应该只将每个元素与其一侧的元素进行比较。 - Daniel Fischer
@Daniel Fischer!在我的情况下,该列表由120个元素组成。超过一半的起始元素已正确排序,但一些末尾元素未排序。我无法理解这种行为。 - Ammar
这是因为你来回交换了。我还没有分析,也许如果你从 k+1 开始内部循环,它就可以工作。然而,正确的方法是创建一个符合你需求的Comparator并使用库排序。或者实现其中一个经典的排序算法。 - Daniel Fischer
4个回答

17

为什么不让您的Helper类实现Comparable接口,然后使用Collections类提供的内置排序方法。

Collections.sort(helperList) 

我认为这会解决问题。此外,这个sort方法是稳定的。

http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort%28java.util.List%29

http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

实现Comparable接口:

public class Helper implements Comparable{

    Iris iris;
    double distance;

    public Helper(Iris iris, double distance) {
        this.iris = iris;
        this.distance = distance;
    }

    public int compareTo(Helper other) {
        return new Double(this.distance).compareTo(new Double(other.distance));

    }
}

如果两个距离小于1,则此compareTo实现将返回0(如果您正确地进行了转换)。返回this.distance < other.distance?-1:this.distance > other.distance?1:0; - jackrabbit
它起作用了。感谢所有人,特别是@Divya对她的建议。 - Ammar
为什么每次比较时都要创建2个Double对象?即使您想这样做,也请使用Double.valueOf()。 - jackrabbit
在添加Java文档的链接时,请尽量包含最新版本 :) - COD3BOY

4

Divya的回答很好,但如果您不想实现Comparable接口,以下内容可能会有所帮助:

Collections.sort(helperList, new Comparator<Helper>() {
    public int compare(Helper helper1, Helper helper2) {
        return Double.compare(helper1.distance, helper2.distance);
    }
})

1
helper1.distance - helper2.distance 返回的是 double 而不是 int。我也犯了同样的错误。 - Divya

1
问题在于循环在交换后失去了距离索引的位置追踪。这个算法应该可以正常工作。
 for(int k = 1; k < helperList.size(); k++) {
    double distance = helperList.get(k).distance;
    int j = k - 1;
    boolean done = false;
    while(!done) {
       Helper temp = helperList.get(j);
       if(temp.distance < distance) {
           helperList.set(j+1, temp);
           j = j - 1;
           if(j < 0) {
              done = true;
           }
       } else {
          done = true;
       }
       helperList.set(j+1, value);
   }
}

谢谢Shawn。我使用了Divya建议的Collections.sort(object),它运行良好。 - Ammar

0

维基百科上关于冒泡排序的文章包含伪代码和一些优化版本。与之比较,看看你哪里出了问题。

冒泡排序是最明显的排序算法之一,但并不是最有效的。为什么不让平台来进行排序呢?java.util.Collections 包含一个sort方法,它允许您提供自己的Comparator。所有这个比较器需要做的就是决定哪个Helper实例应该先出现。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接