2014-01-06 62 views
1

讓我開始用下面的例子:如何在實現比較於Android的對象或Java排序

public void loadList(ArrayList<Calls> list) { 
    List<Calls> calls = new ArrayList<Calls>(); 
    calls.addAll(list); 
} 

其中Calls是一個簡單的類:

public class Calls { 
    public long ms; 
    public name;  
} 

我想了List<Calls>排序以上按照ms字段的升序或降序排列。我遇到了Comparator的一些例子,但仍不清楚。

+0

您嘗試過哪些示例?什麼不明確 –

+0

比較器接口中有一種稱爲比較(對象O1,對象O2)的方法。你必須重寫(實現)它。把你的排序邏輯放在那裏......邏輯很簡單...如果O1.ms TheLostMind

+0

@TheLostMind +1完美地工作.. – ARP

回答

0
class MSComaparator implements Comparator<Calls>{ 

     @Override 
     public int compare(Calls lhs, Calls rhs) { 
      // TODO Auto-generated method stub 
      return lhs.ms-rhs.ms; 
     } 

    } 

    class NameComaparator implements Comparator<Calls>{ 

     @Override 
     public int compare(Calls lhs, Calls rhs) { 
      // TODO Auto-generated method stub 
      return lhs.name.comapreTo(rhs.name); 
     } 

    } 

,並呼籲:

public void loadList(ArrayList<Calls> list) { 
    List<Calls> calls = new ArrayList<Calls>(); 
    calls.addAll(list); 
    Collections.sort(calls,new MSComaparator()) 
} 

,如果你只是想排序基於NS,你可以簡單的實現可比:

public class Calls implements Comparable<Calls>{ 
    public long ms; 
    public name;  
    @Override 
     public int compareTo(Calls another) { 
      // TODO Auto-generated method stub 
      return this.ms>another.ms; 
     } 
} 

而且撥打:

public void loadList(ArrayList<Calls> list) { 
    List<Calls> calls = new ArrayList<Calls>(); 
    calls.addAll(list); 
    Collections.sort(calls) 
} 
1

剛以這種方式實現比較器:

private class CallsComparator implements Comparator<Calls> { 

    @Override 
    public int compare(Calls calls1, Calls calls2) { 
     //Swap calls1 with 2 
     return 1; 
     //Spap 2 with 1 
     return -1; 
     // do nothing 
     return 0; 
    } 

    } 
當然,你必須來取代我使用if條件評論:-)

,並執行比較

Collections.sort(calls, new CallsComparator()); 
2

看下面的例子

class CallsComp implements Comparator<Calls>{ 

    @Override 
    public int compare(Calls c1, Calls c2) { 
     if(c1.getMs() < c2.getMs()){ 
      return 1; 
     } else { 
      return -1; 
     } 
    } 
} 
0

Calls類需要實現Comparable接口並實現compareTo方法:

public class Calls implements Comparable<Calls> { 
    public long ms; 
    public name; 

    @Override 
    public int compareTo(Calls call) { 
     // Prepend a -1 for inverse order 
     return Long.compare(this.ms,call.ms); 
    } 
} 

然後就叫Collections.sort(calls)排序列表。

6

我想這將有助於

public void loadList(ArrayList<Calls> list) { 
    List<Calls> calls = new ArrayList<Calls>(); 
    calls.addAll(list); 

    // Ascending Order 
    Collections.sort(calls, new Comparator<Calls>() { 

     @Override 
     public int compare(Calls o1, Calls o2) { 
      return (int)(o1.ms-o2.ms); 
     } 
    }); 
    // Descending Order 
    Collections.sort(calls, new Comparator<Calls>() { 

     @Override 
     public int compare(Calls o1, Calls o2) { 
      return (int)(o2.ms-o1.ms); 
     } 
    }); 
} 
+0

升序和降序的方法:) – TheLostMind

+0

不要忘記調用youradapter.notifyDataSetChanged();以便更改立即發生。 –

0

@ARP

我知道它來不及回答這個問題,但我已經在我的項目中實現同樣的事情,最近這樣想我應該與你分享

所以你function加入列表,你可以做什麼后里面是

public void loadList(ArrayList<Calls> list) { 
    List<Calls> calls = new ArrayList<Calls>(); 
    calls.addAll(list); 
    Collections.sort(calls, new Comparator<Calls>() { 
       @Override 
       public int compare(Calls o, Calls t1) { 
        return (int) (o.getMS() - t1.getMS()); 
       } 
      }); 
} 

這將根據ms 中的值對ArrayList<Calls>的呼叫進行排序。