1 static List<Integer> intList = Arrays.asList(2,5,7, 3, 1); 2 3 public static void main(String[] args) { 4 5 System.out.println(\"before sort:\"); 6 PrintUtil.showList(intList); 7 System.out.println(\"=========================\"); 8 Collections.sort(intList,new Comparator<Integer>() { 9 10 public int compare(Integer o1, Integer o2) { 11 // 返回值为int类型,大于0表示正序,小于0表示逆序 12 System.out.println(\"o2-o1:\"+(o2-o1)+\"========o2=\"+o2+\"o1=\"+o1); 13 if(o2>o1){ 14 return -1; 15 }else{ 16 return 1; 17 } 18 } 19 }); 20 System.out.println(\"after sort:\"); 21 PrintUtil.showList(intList); 22 23 }
根据需求排序,方法内比较两个对象的参数哪个优先,返回值为int类型,大于0表示正序,小于0表示逆序
继续阅读与本文标签相同的文章
下一篇 :
kNN-Iris分类器(一)
-
我是如何把大象装进冰箱里的……
2026-05-19栏目: 教程
-
关于将Web项目部署到阿里云服务器-5个步骤搞定
2026-05-19栏目: 教程
-
阿里云创业孵化事业部总经理李中雨:经阿里巴巴孵化一年,企业成长6-7倍。
2026-05-19栏目: 教程
-
阿里云学生服务器搭建网站(1)-购买阿里云学生服务器
2026-05-19栏目: 教程
-
提高云资源的利用效率,降低阿里云的成本支出
2026-05-19栏目: 教程
