Collections.sort(positionArray, new Comparator<Integer>() {
@Override
public int compare(Integer lhs, Integer rhs) {
//降序
if(lhs > rhs){
return -1;
}else if(lhs < rhs){
return 1;
}else{
return 0;
}
}
@Override
public int compare(Integer lhs, Integer rhs) {
//降序
if(lhs > rhs){
return -1;
}else if(lhs < rhs){
return 1;
}else{
return 0;
}
}
});
Collections.sort(positionArray, new Comparator<Integer>() {
@Override
public int compare(Integer lhs, Integer rhs) {
//升序
if(lhs > rhs){
return 1;
}else if(lhs < rhs){
return -1;
}else{
return 0;
}
}
});
本文提供了一个使用Java进行数组排序的具体示例,展示了如何通过自定义比较器实现升序和降序排列。
824

被折叠的 条评论
为什么被折叠?



