所以我有這樣的ArrayList:Java如何ArrayList <String>排序/按索引排序?
list.get(0) == "love"
list.get(1) == "foo"
list.get(2) == "make"
list.get(3) == "links"
我要的是:
list.get(0) == "links"
list.get(1) == "make"
list.get(2) == "foo"
list.get(3) == "love"
我都試過,但不工作:
public static void orderDescending(final ArrayList<String> list){
Collections.sort(list, new Comparator<String>() {
public int compare(String s1, String s2) {
Integer i1 = list.indexOf(s1);
Integer i2 = list.indexOf(s2);
return i1.compareTo(i2);
}
});
}
Thanx提前。
你在這裏顯示的是不排序,但正在倒轉...你想扭轉列表嗎? – Hristo 2012-04-11 06:35:32
是的,我想扭轉它。抱歉。 – djcharon 2012-04-11 06:37:03
'Collections.reverse(list);' – Richante 2012-04-11 06:37:50