2012-10-12 102 views
3

好吧假設我有一個如下所示的對象數組: obj(from,to) 我想通過比較from和to來對數組進行排序。什麼我希望做一個例子: 假設我有與這些參數 (0,2)(2,4)(0,3)(4,5)(2,3)
我要的對象是對象按照以下順序排序: (0,2)(0,3)(2,3)(2,4)(4,5)用數字對對數組排序

我想要比較前兩個「from」變量,並將較低的一個前面。如果它們相等,那麼我想要比較第二對數字。要做到這一點,我創建了一個比較方法

public int compare (EdgeI e1, EdgeI e2) { 
    if(e1.from < e2.from) { return -1; } 
    else if(e1.from == e2.from) { 
    if(e1.to < e2.to) { return -1; } 
    else if(e1.to == e2.to) { return 0; } 
    else if(e1.to > e2.to) { return 1; } 
    } 
    return 1; 
} 

這項工作?如果是這樣,我將如何通過數組運行這種排序? 感謝您的幫助。

編輯

public class mySorter implements Comparator <EdgeI> { 

    public int compare(EdgeI e1, EdgeI e2) { 
    if(e1.from < e2.from) { return -1; } 
    else if(e1.from == e2.from) { 
     if(e1.to < e2.to) { return -1; } 
     else if(e1.to == e2.to) { return 0; } 
     else if(e1.to > e2.to) { return 1; } 
    } 
    return 1; 
    } 

    public void sorterM() { 
    Collections.sort(tet2, new mySorter()); 
    } 

} 

我得到的錯誤集合不能得到解決,並且TET2不能得到解決。 Tet2是另一個類中公開的列表。

+0

你正在使用什麼語言和框架? – Neolisk

+0

Java和你的框架是什麼意思? – ellangog

+0

我不是Java專家,但是如果您說C#,則需要指定哪個.NET版本。 – Neolisk

回答

2

你可以做的是創建一個實現Comparator<Edge>類。然後,您可以使用您的比較方法來實現從接口中的方法。

完成此操作後,可以使用Collections.sort()使用比較器對Edge對象列表進行排序。

這將是這個樣子:

import java.util.Collections; 
import java.util.List; 
import java.util.Comparator; 

public class EdgeComparator implements Comparator<Edge> { 
    public int compare(Edge l, Edge r) { ... } 
} 

void yourCode() { 
    List<Edge> edges = ...; 
    Collections.sort(edges, new EdgeComparator()); 
    //edges now contains the sorted edges 
} 

這裏是ComparatorCollections.sort的Javadoc。

如果你有一個數組而不是一個列表,你可以使用Array.sort,就像Collections.sort一樣。

+0

查看我的新編輯 – ellangog

+0

在使用它們之前,您還需要導入類/接口。我將它添加到我的回答中 – cyon

+0

也可以不使用比較器中的sorterM()方法,而是使用具有該列表的其他類中的新EdgeComparator()來實例化比較器。 – cyon

1

你可以讓你的EdgeI對象相媲美,也可以創建一個獨立的比較來處理比較EdgeI對象。在這種情況下,(假設你寫的EdgeI類),更多的面向對象的方法是實現可比。

public class EdgeI implements Comparable<EdgeI> { 
    ... 
    public int compareTo(EdgeI other) { 
     // implement your compare method to use this and other instead of e1 and e2 
    } 
    ... 
} 

然後,您可以使用純Arrays.sort和方法將根據了他們自然排序,這是由繼承compareTo法規定的邊緣進行排序。

EdgeI[] edges = ...; 
Arrays.sort(edges); 

或者,您可以實現一個Comparator,並將其傳遞給sort方法以及目標數組進行排序。遲到了

EdgeI[] edges = ...; 
Arrays.sort(edges, new EdgeComparator()); 
0

但這裏是一個可能實現:

public class EdgeComparator implements Comparator<EdgeI> { 

    public int compare(EdgeI e1, EdgeI e2) { 
     // your method in its exact format 
    } 
} 

然後對它進行排序。您可以避免通過調用Collections.sort方法與主,這就是現實,你會怎樣做內部比較靜態。請注意,有兩個比較器。您可以隨意創建儘可能多的機會,以便您有多種排序方式,即在這種情況下是升序還是降序。這只是要求撥打Collections.sort(edges, ORIGINAL);Collections.sort(edges, REVERSE);

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Comparator; 

public class EdgeI{ 

    private int from; 
    private int to; 

    public EdgeI(int f, int t) 
    { 
     from = f; 
     to = t; 
    } 

    public void setFromTo(int f, int t) 
    { 
     from = f; 
     to = t; 
    } 

    public int getFrom() 
    { 
     return from; 
    } 

    public int getTo() 
    { 
     return to; 
    } 

    public final static Comparator<EdgeI> REVERSE = new Comparator<EdgeI>() 
    { 
    @Override 
    public int compare(EdgeI e1, EdgeI e2) 
    { 
     if(e1.from < e2.from) 
      return 1; 
     if(e1.from > e2.from) 
      return -1; 
     //else they are equal 
     if(e1.to < e2.to) 
      return 1; 
     if(e1.to > e2.to) 
      return -1; 
     //else both edges are equal 
     return 0; 
    } 
    }; 

    public final static Comparator<EdgeI> ORIGINAL = new Comparator<EdgeI>() 
    { 
    @Override 
    public int compare(EdgeI e1, EdgeI e2) 
    { 
      if(e1.from < e2.from) { return -1; } 
      else if(e1.from == e2.from) 
      { 
      if(e1.to < e2.to) { return -1; } 
      else if(e1.to == e2.to) { return 0; } 
      else if(e1.to > e2.to) { return 1; } 
      } 
      return 1; 
    } 
    }; 

    public static void main(String[] args) { 
     ArrayList<EdgeI>edges = new ArrayList<EdgeI>(5); 
     edges.add(new EdgeI(0, 2)); 
     edges.add(new EdgeI(2, 4)); 
     edges.add(new EdgeI(0, 3)); 
     edges.add(new EdgeI(4, 5)); 
     edges.add(new EdgeI(2, 3)); 

     System.out.println("\nBefore sorting:"); 
     for(EdgeI i : edges) 
      System.out.println("("+i.getFrom()+", "+i.getTo()+")"); 

     Collections.sort(edges, ORIGINAL); 

     System.out.println("\nAfter sorting:"); 
     for(EdgeI i : edges) 
      System.out.println("("+i.getFrom()+", "+i.getTo()+")"); 

    } 
} 
/* 
Output on the console: 

Before sorting: 
(0, 2) 
(2, 4) 
(0, 3) 
(4, 5) 
(2, 3) 

ORIGINAL: 
After sorting: 
(0, 2) 
(0, 3) 
(2, 3) 
(2, 4) 
(4, 5) 

REVERSE: 
After sorting: 
(4, 5) 
(2, 4) 
(2, 3) 
(0, 3) 
(0, 2) 
*/ 
+0

沒有聲望爲問題添加評論,但是,@ellangog,您的比較方法的邏輯是正確的。我已經在這裏使用了'ORIGINAL'(減去格式)。 – apcris

+0

感謝您的回答,我設法使用您的答案和cyon的答案來實現它。 – ellangog