刷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. XAF 如何将数据库中Byte array图片显示出来

    问题比较简单,直接上代码. private Image _Cover; [Size(SizeAttribute.Unlimited), ValueConverter(typeof(ImageValue ...

  2. Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片

    Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片 自定义ADPager 自定义水平滚动的ScrollView效仿ViewPager 当遇到要在Vie ...

  3. eclipse安装插件

    http://blog.csdn.net/powmxypow/article/details/11553395

  4. Linux中环境变量文件及配置

    Linux中环境变量文件及配置   一.环境变量文件介绍 转自:http://blog.csdn.net/cscmaker/article/details/7261921 Linux中环境变量包括系统 ...

  5. 【转】使用Reflector和FileDisassembler反编译成项目文件

    转载地址:http://blog.csdn.net/nuaalfm/article/details/2089149 FileDisassembler是Reflector 的一个插件,老外做的东西还真好 ...

  6. 在webapp上使用input:file, 指定capture属性调用默许相机,摄像,录音功能

    ## 在webapp上使用input:file, 指定capture属性调用默认相机,摄像,录音功能 在iOS6下开发webapp,使用inputz之file,很有用 <input type=& ...

  7. java static静态方法的并发性

    在做一个web项目的时候需要做一个通用类去处理一些问题,想到这个类很多地方都有用到,又不想每次都new一个,因此就定义了里面的方法是静态方法,然后又因为多个静态方法都用到了同一个对象,结果定义了一个类 ...

  8. css样式基础三

    css的定位: 其中css中被分为块级元素与行内元素.如块级元素div.hx标签.p元素.行内元素span和strong W3school给出的一切皆为框的定义. 而且可以使用display属性,强行 ...

  9. js手风琴

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  10. Windows普通窗口程序

    2015-10-09 12:55:38 KWindow.h #pragma once #include <windows.h> class KWindow { virtual void O ...