刷leetcodecode时看到一道题需要利用自定义的比较器进行排序,最开始一头雾水,看了API终于懂了~

Arrays.sort(T[] a,Comparator<? super T> c)可以根据比较器的compare方法对数组进行排序,compare方法的不同实现对应着不同的排序准则;

可以看到API中关于compare方法的解释如下:

int compare(T o1,T o2)
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

In the foregoing description, the notation sgn(expression) designates the mathematical signum function, which is defined to return one of -1, 0, or 1 according to whether the value of expression is negative, zero or positive.

The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that sgn(compare(x, z))==sgn(compare(y, z)) for all z.

It is generally the case, but not strictly required that (compare(x, y)==0) == (x.equals(y)). Generally speaking, any comparator that violates this condition should clearly indicate this fact. The recommended language is "Note: this comparator imposes orderings that are inconsistent with equals."

参数:
o1 - the first object to be compared.
o2 - the second object to be compared. 
也就是说compare方法根据其返回值确定比较对象的大小,如果返回值为正,认为o1>o2;返回值为负,认为o1<o2;返回值为0,认为两者相等;
public class SortByComparator{
//升序排列
public static void sortAscend(Integer[] a){
Arrays.sort(a, new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return a.compareTo(b);
}
});
}
//降序排列,如果b.compareTo(a)>0,则compare方法根据其返回值认为a>b,与自然比较结果相反
public static void sortDescend(Integer[] a){
Arrays.sort(a, new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return b.compareTo(a);
}
});
}
public static void main(String[] args) {
Integer[] a={1,2,3};
//升序排列
sortAscend(a);
for(int num:a)
System.out.println(num);
//降序排列
sortDescend(a);
for(int num:a)
System.out.println(num); }
}

输出

1
2
3
3
2
1

注:Arrays.sort(T[] a,Comparator<? super T> c)默认为升序排列

comparator接口与compare方法的实现的更多相关文章

  1. Comparable和Comparator接口是干什么的?列出它们的区别。

    Comparable和Comparator接口是干什么的?列出它们的区别. Java提供了只包含一个compareTo()方法的Comparable接口.这个方法可以个给两个对象排序.具体来说,它返回 ...

  2. Java—集合框架 Collections.sort()、Comparable接口和Comparator接口

    Collentions工具类--java.util.Collections Collentions是Java集合框架中,用来操作集合对象的工具类,也是Java集合框架的成员,与List.Map和Set ...

  3. Java中Comparator接口和Comparable接口的使用

    普通情况下在实现对对象元素的数组或集合进行排序的时候会用到Comparator和Comparable接口,通过在元素所在的类中实现这两个接口中的一个.然后对数组或集合调用Arrays.sort或者Co ...

  4. Comparable和Comparator接口是干什么的?列出它们的区别

    Java提供了只包含一个compareTo()方法的Comparable接口.这个方法可以个给两个对象排序.具体来说,它返回负数,0,正数来表明输入对象小于,等于,大于已经存在的对象. Java提供了 ...

  5. java实现Comparable接口和Comparator接口,并重写compareTo方法和compare方法

    原文地址https://segmentfault.com/a/1190000005738975 实体类:java.lang.Comparable(接口) + comareTo(重写方法),业务排序类 ...

  6. 比较器:Compare接口与Comparator接口区别与理解

    一.实现Compare接口与Comparator接口的类,都是为了对象实例数组排序的方便,因为可以直接调用 java.util.Arrays.sort(对象数组名称),可以自定义排序规则. 不同之处: ...

  7. Java TreeSet集合排序 && 定义一个类实现Comparator接口,覆盖compare方法 && 按照字符串长度排序

    package TreeSetTest; import java.util.Iterator; import java.util.TreeSet; import javax.management.Ru ...

  8. java学习-Comparable<Integer>接口方法的实现

    基本数据类型的包装类Integer, Float, Double,Long,Byte等都实现的Comparable接口,用于列表List或数组arrays的排序 Comparable<Integ ...

  9. 关于comparator接口和comparable接口以及它们各自的方法compare()和compareTo()

    在今天做的LeetCode的题中有两道都出现了利用接口实现对象的排序.两题的相关链接: 1.利用comparable接口对对象排序 2.利用comparator接口实现排序 因为之前都没接触过这两个接 ...

随机推荐

  1. 详解:数据库名、实例名、ORACLE_SID、数据库域名、全局数据库名、服务名及手工脚本创建oracle数据库

    数据库名.实例名.数据库域名.全局数据库名.服务名 , 这是几个令很多初学者容易混淆的概念.相信很多初学者都与我一样被标题上这些个概念搞得一头雾水.我们现在就来把它们弄个明白. 一.数据库名 什么是数 ...

  2. dev GridControl实现拖拽

    一.示例说明 以gridControl1和gridControl2为例,从gridControl1拖拽行到gridControl2中去. 二.属性设置 gridControl2.AllowDrop = ...

  3. webpack react基础配置二 热加载

    用到 webpack-dev-server  先安装,注意 装到全局 还是本项目我也没注意  因为之前一直报错,有很小可能是安装到本地解决了,或者是我网络问题: 装到全局:$ npm install ...

  4. SQLServer控制用户访问权限表

    连接地址:http://www.cnblogs.com/yxyht/archive/2013/03/22/2975880.html 一.需求 在管理数据库过程中,我们经常需要控制某个用户访问数据库的权 ...

  5. 阿里云CentOS6.5搭建服务器JDK+tomcat+MySQL

    阿里云ECS,计划安装jdk:MySQL:tomcat: 一.yum Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的S ...

  6. https 单向认证和双向认证配置

    HTTPS 是我们开发中经常用到的通信加密技术,能有效保护我们网络访问中的安全,本文主要讲解单向 和 双向 https 的配置.关于https 的实现原理在这里我就不赘述了,附上阮一峰老师的关于htt ...

  7. 【前端】Web前端学习笔记【2】

    [2016.02.22至今]的学习笔记. 相关博客: Web前端学习笔记[1] 1. this在 JavaScript 中主要有以下五种使用场景 在全局函数调用中,this 绑定全局对象,浏览器环境全 ...

  8. getopt()和getopt_long()用法

    参考链接: http://home.eeworld.com.cn/home.php?mod=space&do=blog&id=43897&uid=101752 http://b ...

  9. Command: sl (Steam Locomotive)

    You might be aware of command 'ls' the list command and use it frequently to view the contents of a ...

  10. MacOS X Terminal中设置代理

    MacOS X中,即使在网络设置中配置了代理连接,Terminal中也不能访问不可描述的东西,需要额外设置. 我用的是Shadowsocks,使用privoxy搭建了本地的代理服务器,地址是http: ...