2015-11-07 84 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; 
} 
相关问题