我建立了一对夫妇:
public class Couple<E,K> {
E first; K second;
Couple(E first, K second){
this.first = first;
this.second = second;
}
public E getFirst(){
return first;
}
public K getSecond(){
return second;
}
}
ArrayList<Couple<String, Integer>>
ArrayList<Couple<URL, Integer>> a = ArrayList<Couple<URL, Integer>>();
a=fillArray();
我想通过整数的第二个元素的值来排序'a'。
答案 0 :(得分:1)
这样做
您的SimpleComparator
class SimpleComparator implements Comparator<Couple<URL, Integer>> {
public int compare(Couple<URL, Integer> a, Couple<URL, Integer> b) {
return a.second.compareTo(b.second);
}
}
你的排序
Collections.sort(a,new SimpleComparator()); // now a is sorted based on second value.
答案 1 :(得分:0)
a.sort(new Comparator<Couple<URL, Integer>>() {
public int compare(Couple<URL, Integer> a, Couple<URL, Integer> b) {
return a.second.compareTo(b.second); //assuming a.second is not null
}
});