2012-08-02 58 views
3

Hello stackoverflow社區! 我是新來的這些論壇,也相當新的java和android編程 - 這恰好是我的問題的對象 - 所以任何失誤提前抱歉!根據其字段中的1對對象列表排序

我的問題是排序。我正在尋找一種方法來基於我選擇的字段對對象進行排序(不是基於第一個字段進行排序,然後是下一個等,通過比較器鏈接進行排序)。我相信我已經找到了解決我的問題:

https://stackoverflow.com/a/5113108/1549672

,但我有麻煩卻越來越這項工作。我懷疑我可能因爲缺乏Java經驗而錯過了一些東西,所以歡迎任何幫助!

這裏就是我想:

正如我的講座

public class ItemLocation { 
String title; 
int id; 
} 

正如我的功能 -

public void sort(final String field, List<ItemLocation> itemLocationList) { 
    Collections.sort(itemLocationList, new Comparator<ItemLocation>() { 
     @Override 
     public int compare(ItemLocation o1, ItemLocation o2) { 
      if(field.equals("title")) { 
       return o1.title.compareTo(o2.title); 
      } else if(field.equals("id")) { 
       return Integer.valueOf(o1.id).compareTo(o2.id); 
      } 
      return 0; 
     } 
    }); 
} 

使用這些,可能有人可能給使用這種方法的一個實例?我試圖填充一個ArrayList並對其進行排序,但無濟於事。

感謝您的幫助!

回答

8

如果不相等,則不應從Comparator.compare方法返回0。這是「好」的合同,但不完全鼓勵,從API文檔:

這是一般的情況,但並不嚴格要求(比較(X, Y)== 0)==( x.equals(Y))。一般來說,任何違反此條件的比較器都應清楚地表明這一事實。 推薦的語言是「注意:此比較器強制排列 與等號不一致。」


在我看來,你應該返回爲每個字段,而不是特定的Comparator

Comparator<ItemLocation> titleComparator = new Comparator<ItemLocation>() { 
    @Override 
    public int compare(ItemLocation o1, ItemLocation o2) { 
     return o1.title.compareTo(o2.title); 
    } 
} 

Comparator<ItemLocation> idComparator = new Comparator<ItemLocation>() { 
    @Override 
    public int compare(ItemLocation o1, ItemLocation o2) { 
     return Integer.valueOf(o1.id).compareTo(o2.id); 
    } 
} 

public void sort(final String field, List<ItemLocation> itemLocationList) { 

    final Comparator<ItemLocation> comparator; 

    if(field.equals("title")) { 
     comparator = titleComparator; 
    } else if (field.equals("id")) { 
     comparator = idComparator; 
    } else { 
     throw new IllegalArgumentException("Comparator not found for " + field); 
    } 

    Collections.sort(itemLocationList, comparator); 
} 
+0

謝謝先生!我包括返回0的原因是因爲日食告訴我,該方法必須返回一個整數,我不知道還有什麼要放!這是一個更簡單的實現來理解來自C++背景!再次感謝你! – user1549672 2012-08-02 20:56:22

0

您可以發佈不工作調用代碼?我看不出你提供的代碼有什麼明顯的錯誤。

首先,你可以嘗試是把一個額外的其他情況下,像這樣:

else { 
    throw new IllegalArgumentException("Unrecognised field name"); 
} 

此刻,如果你有在你的調用代碼一個錯字,比較總是返回0,這將離開列表未排序。

路過現場將宣佈一個枚舉的更健壯的方式:

enum ItemLocationField { 
    TITLE, 
    ID 
} 

那麼你的條件將成爲:

if (field == ItemLocationField.TITLE) 

等。這將減少發生錯字的可能性(編譯器會告訴你,如果你這樣做)。

0

除了returning 0和比較參數equals我沒有看到任何錯誤。您可以通過throwing RuntimeException而不是returning 0來改善它,並使用equalsIgnoreCase而不是equals方法,最好忽略參數的情況。

public static void sort(final String field, List<ItemLocation> itemLocationList) { 
    Collections.sort(itemLocationList, new Comparator<ItemLocation>() { 
     @Override 
     public int compare(ItemLocation o1, ItemLocation o2) { 
      if(field.equalsIgnoreCase("title")) { 
       return o1.title.compareTo(o2.title); 
      } else if(field.equalsIgnoreCase("id")) { 
       return Integer.valueOf(o1.id).compareTo(o2.id); 
      }else 
       throw new IllegalArgumentException("Invalid Parameter ."); 
     } 
    }); 
} 
+0

我認爲這主要是由於我缺乏java的經驗...... Eclipse只是告訴我,實現必須返回一個整數! – user1549672 2012-08-02 21:00:02

0

如果要排序的基礎只有一個屬性的物體上,然後用Collections.sort(List<T> list)

2.java.lang.Comparable接口沿着如果要排序的對象在以上屬性,然後去 java.util.Comparator接口連同Collections.sort(List<T> list, Comparator<? super T> c)

+0

謝謝!我主要是在尋找一種基於包含MULTIPLE屬性的對象的ONE屬性進行排序的方法! – user1549672 2012-08-02 20:58:10

+0

亞..這就是我的第一點建議.....排序基於一個屬性。第二點是附加知識 – 2012-08-03 04:55:59