2015-11-07 85 views
0

我有兩個ArrayList<JLabel>如何根據另一個ArrayList對ArrayList進行排序?

ArrayList<JLabel> label1 = new ArrayList<JLabel>(); 
ArrayList<JLabel> label2 = new ArrayList<JLabel>(); 

label1包含名稱(如 「約翰」 或 「汽車」)和label2包含等級(如 「8.5」 或 「10.0」)。我想按評級排序這兩個列表。

我用Collections.sort(label2, new Sort());排序label2,但我不知道如何排序在exaclty以同樣的方式(使用label2對象)label1。這裏是我的Comparator職業:

class Sort implements Comparator<JLabel>{ 
    public int compare(JLabel o1, JLabel o2) { 
     Double a = Double.parseDouble(o1.getText()); 
     Double b = Double.parseDouble(o2.getText()); 
     return b.compareTo(a); 
    } 
} 

任何想法?

+1

你能舉一個例子輸入輸出嗎? – Alexander

回答

2

既然你有沒有這兩個名單,我認爲你應該考慮在一個類中封裝了兩個值可達之間定義的關係:

class Foo { 

    private String name; 
    private double rating; 

    public Foo(String name, double rating){ 
     this.name = name; 
     this.rating = rating; 
    } 

    // getters & setters 
} 

然後,您可以有List<Foo>和排序基於的價值列表評分。

在Java 8,這將是爲調用sort()方法,這是目前在List提供簡單,用一個lambda表達式中傳遞。

+2

除了這個答案我會補充說,你可以使用方法引用進行排序。 'list.sort(Comparator.comparingDouble(Foo :: getRating))' –

+0

它的工作原理!非常感謝你 :)。 – Ambu

0

讓我們來解決這個使用Map。我們首先定義一個比較排序按值下降,就像這樣:

class NumericComparator extends Comparator { 
    Map map; 
    public NumericComparator(Map map) { 
     this.map = map; 
    } 

    public int compare(Object o1, Object o2) { 
     return ((Integer) map.get(o2)).compareTo((Integer) map.get(o1)); 
    } 
} 

我們將定義名稱爲重點,並作爲評價值。像這樣:

Map<String, Double> carMap = new HashMap<>(); 
//we can populate the map like so: 
carMap.put("John", 10d); //works nicely in loops 

然後我們採取無序地圖,然後將它的所有值轉換爲TreeMap,實現我們前面定義的NumericComparator

NumericComparator nc = new NumericComparator(carMap); 
Map<String, Double> sortedMap = new TreeMap(nc); 
sortedMap.putAll(carMap); 
0

首先,請編程到List界面。接下來,我假設你可以使用apache庫,apache commons-lang中有Pair<L, R>(或者你可以實現你自己的元組類;或者使用另一個類似this one)。無論如何,您需要修改Comparator才能在右側進行操作(並且我個人喜歡使用左側打破關係)。最後,如果您想使用label1label2,則需要將值複製回來(排序後)。類似的,

List<JLabel> label1 = new ArrayList<>(); 
List<JLabel> label2 = new ArrayList<>(); 
// ... 
List<Pair<JLabel, JLabel>> pairs = new ArrayList<>(); 
int len = label1.size(); 
if (len == label2.size()) { 
    for (int i = 0; i < len; i++) { 
     pairs.add(Pair.of(label1.get(i), label2.get(i))); 
    } 
    Collections.sort(pairs, new Comparator<Pair<JLabel, JLabel>>() { 
     @Override 
     public int compare(Pair<JLabel, JLabel> o1, 
       Pair<JLabel, JLabel> o2) { 
      double a = Double.parseDouble(o1.getRight().getText()); 
      double b = Double.parseDouble(o2.getRight().getText()); 
      int ret = Double.compare(a, b); 
      if (ret != 0) { 
       return ret; 
      } // if ret is 0, it's a tie. 
      return o1.getLeft().getText() 
        .compareTo(o2.getLeft().getText()); 
     } 
    }); 
    // ... only if you need to use label1 and label2 after the sort. 
    for (int i = 0; i < len; i++) { 
     label1.set(i, pairs.get(i).getLeft()); 
     label2.set(i, pairs.get(i).getRight()); 
    } 
} 
0

好問題 - 對我來說,當在棋盤遊戲應用中通過擲骰子排序玩家順序時也是一件麻煩事。

先前在新的粗體對象或映射中綁定這兩個列表的答案(當所有字符串都是唯一的時候)都很好。

但是,如果你確實想要更簡單的東西,那麼這就是這個。
我原本是在沒有引用原始比較列表的深層副本的情況下做的,它獲得了一些排序權和一些奇數條目的混合。 。 。最終我看到了光。 還要確保將排序代碼放入私有方法中,以便通過對Comparator的覆蓋比較(..)方法進行排序來訪問兩個列表label1 & label2。

. . . . 
. . . . 

ArrayList<JLabel> label1 = new ArrayList<JLabel>(); // String list 
ArrayList<JLabel> label2 = new ArrayList<JLabel>(); // Double list 
// Now sort label1 list according to respective double list entries where 
// the double list is sorted in descending order using static method : 
sortByValue(label1, label2); 

. . . . 
. . . . 

// Check original & sorted string lists : 
System.out.println("Original list of strings : " + label11); 
System.out.println("Sorted list of strings : " + label1); 

. . . . 
. . . . 

// Private static method to facilitate access to list being sorted and 
// list used as a sort criterion by the code in the sort Comparator's 
// overriding compare(..) method. 
private static void sortByValue(ArrayList<JLabel> label1, 
           ArrayList<JLabel> label2) 
{ 
    // Deep copy original string list : 
    ArrayList<JLabel> label11 = new ArrayList<JLabel>(label1); 
    // Sort label1 list by value in corresponding label2 list element : 
    Collections.sort(list1, new Comparator<JLabel>() 
    { 
    @Override 
    public int compare(JLabel x, JLabel y) 
    { 
     // Sort label1 by descending order of corresponding label2 values : 
     return 
     (Double.parseDouble(label2.get(label11.indexOf(y)).getText()) 
     -Double.parseDouble(label2.get(label11.indexOf(x)).getText())) 
     .intValue(); 
    } 
    }); 
return; 
} 
相關問題