2016-05-16 40 views
0

我有一個包含3個鏈表的ArrayList。第一個包含String,secound整數和第三個浮點數。 每個列表的第一個值是相互關聯的,secound等等。現在對包含3個鏈表的Arraylist進行排序

List<List> calculation = new ArrayList<List>(); 
ArrayList<String>candidate = new ArrayList<String>(); 
ArrayList<Integer> frequency = new ArrayList<Integer>(); 
ArrayList<Float> score = new ArrayList<Float>(); 
calculation.add(candidate); 
calculation.add(frequency); 
calculation.add(score); 

我想排序雙列表(升序或降序),當然在其他兩個表中的值應與第三列表的排序變化。

我該怎麼做?也許我的構造(包含三個列表的Arraylist)不是最好的方法。我也常常爲其他apparoaches;)

+4

爲什麼不創建一個包含候選項,頻率和分數的類,並對該類的單個ArrayList進行排序? – Eran

+0

您可以按照所有這些,而不是唯一的比分的,對於爲例,當你for循環裏面做你的嵌套的排序,你可以簡化掉也值中的其他數組,而不是隻有一個在你的,如果你需要我可以給你更詳細的答案 –

+0

我已經在想這件事,但我想用一些現有的排序功能;)我認爲在你的建議中,我將不得不編寫自己的排序方法或覆蓋現有的... – Tobi123

回答

0

你需要一個總體的數據結構列表重新排序,以保持凝聚力。

我個人它們移動到Apache的Triple的列表,然後你可以定義一個比較以上類似。

0

我選擇帶有額外的類的方式。工作正常,謝謝你們!

public class Result { 

    String candidate; 
    int frequency; 
    double score; 

    public Result (String candidate, int frequency, double score){ 
     this.candidate=candidate; 
     this.frequency=frequency; 
     this.score=score; 
    } 
}