java排序Comparator基本用法

java中进行排序时,通过实现Comparator接口,可以对数组进行任意排序,非常方便。使用是先写一个类实现Comparator接口,实现该接口的方法compare,该方法返回int型数据,如果返回值大于0,表示需要交换两个元素,返回值小于0表示顺序正确,返回值等于0表示两个数相等(java使用的是归并排序,归并排序时一种稳定排序,两个数相等时会按照原来的顺序排列)。
下面给出一些面试用到的排序方法,包括按绝对值排序、把0写在最前面、还有排名次等方法。

import java.util.Arrays;
import java.util.Comparator;

/**
 * 按绝对值进行排序
 */
class AbsComprator implements Comparator<Integer>
{
    @Override
    public int compare(Integer o1, Integer o2) {        
        return Math.abs(o1)-Math.abs(o2);
    }
}
/**
 * 把0排在前面,后面按自然排序
 */
class ZeorFirstComprator implements Comparator<Integer>
{
    @Override
    public int compare(Integer o1, Integer o2) {        
        if(o1==0)
            return -1;
        if(o2==0)
            return 1;
        return o1.compareTo(o2);
    }
}
/**
 * 把0排在前面,其他顺序不动。
 * 不推荐这么做,可以从后往前一趟遍历搞定,更快捷高效。
 */
class ZeorFirstComprator2 implements Comparator<Integer>
{
    @Override
    public int compare(Integer o1, Integer o2) {        
        if(o1==0)
            return -1;
        if(o2==0)
            return 1;
        return 0;
    }
}

/**
 * 原数组不动,对数组元素的索引进行排序。
 */
class IndexComprator implements Comparator<Integer>
{
    //原数组
    private final int[] arr;
    public IndexComprator(int []arr) {
        this.arr = arr;
    }
    @Override
    public int compare(Integer o1, Integer o2) {        
        //根据索引对原值比较
        return arr[o1] - arr[o2];
    }
}
public class Main {
    //把int型数据转换成Integer类型数组
    public static Integer[] toIntegerArray(int[] array)
    {
        Integer[] result = new Integer[array.length];
        for(int i=0;i<array.length;i++)
        {
            result[i] = array[i];
        }
        return result;
    }
    public static void main(String[] args) {
        int []arr = {1,3,10,5,0,-4,-3,0,-1,-5,9,-10};
        //按绝对值排序
        Integer []arr1 = toIntegerArray(arr);
        Arrays.sort(arr1, new AbsComprator());
        System.out.println(Arrays.toString(arr1));
        //把0写在最前面,其他进行自然排序
        Integer []arr2 = toIntegerArray(arr);
        Arrays.sort(arr2, new ZeorFirstComprator());
        System.out.println(Arrays.toString(arr2));
        //把0写在前面,其他不动
        Integer []arr3 = toIntegerArray(arr);
        Arrays.sort(arr3, new ZeorFirstComprator2());
        System.out.println(Arrays.toString(arr3));

        //对索引进行排序
        Integer []arr4 = new Integer[arr.length];
        for(int i=0;i<arr.length;i++)
            arr4[i] = i;//创建索引
        Arrays.sort(arr4, new IndexComprator(arr));
        System.out.println(Arrays.toString(arr4));
        for(int i=0;i<arr4.length;i++)
        {
            //按索引从小到大打印
            System.out.print(arr[arr4[i]]+", ");
        }
        System.out.println();
        //有了索引排序很容易计算出名次
        Integer[] arr5 = new Integer[arr.length];
        for(int i=0;i<arr.length;i++)
        {
            //计算名次
            arr5[arr4[i]] = i+1;
        }
        System.out.println(Arrays.toString(arr));
        //从小到大排名
        System.out.println(Arrays.toString(arr5));
    }
}

来一道编程题目,
南阳理工的1149题’
题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=1149

描述
给出一个长为len的字符串str,把字符串的首尾相连,然后以每个字符为起点,顺时针遍历每个字符,得到len个新的字符串,然后把这len个字符串按照字典序从小到大的顺序进行排序,取出排完序后的每个字符串的最后一个字符,形成一个新的字符串S。求S。
如下图。假设str=“topcoder”,则得到的新的字符串有
topcoder, opcodert, pcoderto, codertop, odertopc, dertopco, ertopcod, rtopcode,按字典序排完序为
codertop, dertopco, ertopcod, odertopc, opcodert, pcoderto, rtopcode, topcoder,
则取出每个字符串最后一个字符之后形成的字符串为podctoer,即S = “podctoer”。

输入
多组测试数据。
每组测试数据包括一个字符串,长度不超过100000。字符串由所有可打印字符组成。
输出
每组数据占一行,输出S。
样例输入
topcoder
I LOVE YOU
样例输出
podctoer
IEVU YLOO


import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class MyComparator implements Comparator
{
    public final char[] arr;
    public MyComparator(char[] array) {
        this.arr = array;
    }
    @Override
    public int compare(Object o1, Object o2) {
        Integer a = (Integer)o1;
        Integer b = (Integer)o2;
        int len = arr.length>>1;
        for(int i=a,j=b,k=0;k<len;++i,++j,++k)
        {
            if(arr[i]>arr[j])
                return 1;
            else if(arr[j]>arr[i])
                return -1;
        }
        return 0;
    }

}

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNextLine())
        {
            String s = in.nextLine();
            char arr[] = s.toCharArray();
            char arr2[] = new char[arr.length<<1];
            System.arraycopy(arr, 0, arr2, 0, arr.length);
            System.arraycopy(arr, 0, arr2, arr.length, arr.length);
            Integer [] arr3 = new Integer[arr.length];
            for(int i=0;i<arr3.length;i++)
                arr3[i] = i;
            Arrays.sort(arr3,new MyComparator(arr2));
            for(int i=0;i<arr3.length;i++)
            {
                System.out.print(arr2[arr3[i]+arr.length-1]);
            }
            System.out.println();
        }
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值