import java.util.ArrayList;
import java.util.Comparator;
public class Sort {
public static void main(String[] args) {
ArrayList otherlist = new ArrayList();
otherlist.add(1);
otherlist.add(9);
otherlist.add(3);
otherlist.sort(new NumberComparator());
System.out.println(otherlist);
}
}
class NumberComparator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
//return o1 - o2;//升序排序
return o2-o1; //降序排序
//return 0;两数相等
}
}
集合排序代码
最新推荐文章于 2024-09-01 15:30:33 发布