总结一下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. CSS VISUAL RULES

    CSS VISUAL RULES Review Visual Rules Incredible work! You used CSS to alter text and images througho ...

  2. MySQL大数据量分页查询方法及其优化

    MySQL大数据量分页查询方法及其优化   ---方法1: 直接使用数据库提供的SQL语句---语句样式: MySQL中,可用如下方法: SELECT * FROM 表名称 LIMIT M,N---适 ...

  3. docker之container

    转自:https://www.cnblogs.com/jsonhc/p/7760144.html 运行一个container的本身就是开启一个具有独立namespace的进程 进程有自己的网络,文件系 ...

  4. es查询时报 Data too large

    报错如下: 原因: https://www.cnblogs.com/jiu0821/p/6526930.html 参数 indices.fielddata.cache.size 控制有多少堆内存是分配 ...

  5. C# 保证数据长度相同

    /// <summary> /// 保证数据长度相同 /// </summary> /// <param name="obj"></par ...

  6. 根据获取的窗口句柄遍历窗口Edit控件

    网上说遍历窗口控件有两种方法: 1),使用EnumChildWindows,没有深究,     学习网址如下:http://blog.sina.com.cn/s/blog_60ac1c4b010116 ...

  7. cakephp 利用Pushapi扩展 进行app 消息推送

    public function push_designer_app($params) { $this->layout = false; $this->autoRender = false; ...

  8. Spring的一些资源

    1.http://spring.io/ 2.http://projects.spring.io/spring-framework/

  9. java swing示例

    该范例主要是JFrame(框架)和Jpanel(画板),在Jpanel容器上添加控件,然后再把Jpanel放进JFrame的容器里面. FrameDemo.java import java.awt.D ...

  10. hdoj1004(查找众多字符串中个数最多的字符串)

    Let the Balloon Rise. 最近开始刷hdoj,想通过写博客做做笔记,记录写过代码. Problem Description Contest time again! How excit ...