总结一下java 中的两种排序工具:

Arrays.sort() : 主要针对 数组类型排序,如果数组里面的元素是对象,要按对象属性排序的话,需要重写 Comparator() 函数,重写里面的  int compare()函数;

Collections.sort(): 主要是针对集合排序,如:list。 当然如果数组 array[i] 的每项是一个list 也可以直接用。与Arrays.sort()一样,重写 Comparator()函数。

注意:两种方式重写的函数方式是一样的,单词不要写错哟: Comparator,compare


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; public class Main1 {
public static void main(String[] args) {
man[] mans = new man[3];
mans[0] = new man(1);
mans[1] = new man(24);
mans[2] = new man(5); Comparator<man> cmp = new My_Comparator ();
//对象数组排序:
Arrays.sort(mans, cmp);
for(man i : mans) {
System.out.print(i.a + ", ");
}
System.out.println("---------"); //对象链表排序:
List<man> list = new ArrayList<>();
list.add(new man(3));
list.add(new man(2));
list.add(new man(1));
// Collections.sort(list, new Comparator<man>() {
// public int compare(man x, man y) {
// return y.a - x.a;
// }
// });
// 将上面的简化为下面的:
Collections.sort(list, cmp);
for(man i : list) {
System.out.print(i.a + "; ");
}
}
}
class My_Comparator implements Comparator<man>{
@Override
public int compare(man o1, man o2) {
return o2.a - o1.a;
}
}
class man{
public int a;
man(int x){
this.a = x;
}
}

  

39-java中Arrays.sort 和 collections.sort()总结的更多相关文章

  1. Arrays.sort 与 Collections.sort

    代码如下: package com.wangzhu.arrays; import java.util.Arrays; import java.util.Collections; public clas ...

  2. 48- java Arrays.sort和collections.sort()再次总结

    今天又碰到一个新BUG,记下来. 一直报空指针异常,我就很奇怪了,怎么就空指针了呢,我输出时,也能输出东西呀. 原来Arrays.sort() 和 Collections.sort() 都是对整个数组 ...

  3. Arrays.sort和Collections.sort实现原理解析

    Arrays.sort和Collections.sort实现原理解析 1.使用 排序 2.原理 事实上Collections.sort方法底层就是调用的array.sort方法,而且不论是Collec ...

  4. sort()排序 collections.sort();

    1.main方法: public class Test { public static void main(String[] args) { /** * * sort()方法详解 * 1.Collec ...

  5. Java中Arrays.sort()和Collections.sort()

    1.简单示例 sort方法的使用非常的简单明了,下面的例子中,先定义一个比较Dog大小的Comparator,然后将其实例对象作为参数传给sort方法,通过此示例,你应该能够快速掌握Arrays.so ...

  6. 关于Java中Arrays.sort()方法TLE

    最近一直在练用Java写题,今天无意发现一道很简单的二分题(链接),我一开始是直接开int[]数组调用Arrays.sort()去排序,没想到TLE了,原来是因为jdk中对于int[]的排序是使用快速 ...

  7. 5.4 集合的排序(Java学习笔记)(Collections.sort(),及Arrays.sort()底层分析)

    1.Comparable接口 这个接口顾名思义就是用于排序的,如果要对某些对象进行排序,那么该对象所在的类必须实现 Comparabld接口.Comparable接口只有一个方法CompareTo() ...

  8. java中Arrays.sort()对二位数组进行排序

    int [][]a = new int [5][2]; //定义一个二维数组,其中所包含的一维数组具有两个元素 对于一个已定义的二位数组a经行如下规则排序,首先按照每一个对应的一维数组第一个元素进行升 ...

  9. 集合(一)-Java中Arrays.sort()自定义数组的升序和降序排序

    默认升序 package peng; import java.util.Arrays;  public class Testexample { public static void main(Stri ...

随机推荐

  1. THE BOX MODEL

    Review In this lesson, we covered the four properties of the box model: height and width, padding, b ...

  2. python流程控制while和if

    流程控制 1.流程控制if if的4种语法 语法1:   #结构形式 #if条件: # 代码1 # 代码2 # 代码3 # ... key_bak=123 key=int(input('key:')) ...

  3. 转:用JS获取地址栏参数的方法(超级简单)

    转载链接: http://www.cnblogs.com/fishtreeyu/archive/2011/02/27/1966178.html 方法一:采用正则表达式获取地址栏参数:( 强烈推荐,既实 ...

  4. jgGrid

    jqGrid的表格加载 function GetGrid() { var selectedRowIndex = 0; var $gridTable = $("#gridTable" ...

  5. 启动 idea 编译报错 kotlin

    怀疑是插件问题. 重新删除了.  C:\Users\user  里面inteliJIdea2018.3 缓存 ,解决

  6. vue router 懒加载实现

    在vue-cli脚手架中router文件夹中有index.js文件,里面的内容是 import Vue from 'vue'import Router from 'vue-router'import ...

  7. spring boot IDEA 开发微服务(二)

    https://www.cnblogs.com/spaceud/p/8097934.html 参考以上微博,自己实际操作步骤如下: 1~开发准备 JDK:1.8 Spring Boot:2.1.0 S ...

  8. js 创建对象的几种方法

    1. 使用object创建 var person = new Object(); person.name = "Tom"; person.age = "29"; ...

  9. SSL、TLS中间人攻击

    生成私钥 生成一个ca.key私钥签名 然后用私钥生成一个伪造证书服务器的根证书 生成一个伪造证书服务器的根证书ca.crt 在攻击主机启动路由转发功能 或者echo 1 > /proc/sys ...

  10. mac通过命令行获取证书和配置文件过期时间

      背景:ios打包证书的profile配置文件过期了,导致以前已经打完的测试包不能安装.所以需要加上检测机制,在打包时提示证书是否将要过期,如果要过期了给出提示   方案: 1.查找profile配 ...