因为我刚开始使用 JAVA,所以我很好奇在 JAVA 中(对于 ArrayLists)实现排序的最佳选择是什么。下面我提供了我的 PHP 代码。
public int cmp($a, $b) {
if ( $a[0] < $b[0] ) return 1;
else if ( $a[0] > $b[0] ) return -1;
else if ( $a[1] < $b[1] ) return 1;
else if ( $a[1] > $b[1] ) return -1;
else return 0;
}
$selected = array();
for ($i=0; $i<$len; $i++) {
$rank = getRank();
$cub = getCub_len();
$selected[] = array($rank,$cub);
}
uasort($selected, 'cmp')
嗯,我用JAVA写了下面的代码:
ArrayList<ArrayList<Double>> selected = new ArrayList<ArrayList<Double>>();
ArrayList<Double> rank = new ArrayList<Double>();
ArrayList<Double> cub = new ArrayList<Double>();
for (int i=0; i<len; i++) {
rank.add(getRank(i));
cub.add(getCub(i));
}
selected.add(0,rank);
selected.add(1,cub);
如何以正确的方式对selected
进行排序(类似于PHP函数cmp
)?
最佳答案
Collections.sort(a);
关于java - 排序数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15666382/