JAVA中sort函数的使用方法的个人总结

说到排序的话,第一个想到的是sort函数。但是吧,java里的sort函数与c++里的sort函数的使用方法有那么些不同。

在这里,本人将sort函数的使用方法大致罗列出来。


首先:sort函数的基本格式(默认排序为升序排序)

Arrays.sort(数组名,起始下标,终止下标); 


我们举个简单的例子

    import java.util.*;  
    import java.util.Arrays;  
    public class Main {  
        public static void main(String[] args) {  
              
            Scanner in=new Scanner(System.in);  
              
            while(in.hasNext())  
            {  
                int num[]=new int[100];  
                  
                int n;///输出n个数  
                n=in.nextInt();  
                  
                for(int i=0;i<n;i++)  
                {  
                    num[i]=in.nextInt();  
                      
                }  
                  
                Arrays.sort(num,0,n);///排序部分  
                  
                for(int i=0;i<n;i++)  
                {  
                    System.out.println(num[i]);  
                      
                }  
            }  
      
        }  
    }  

这个代码输入n个数,并把他们从小到大排序,运行结果如下。


大家注意一下,这里的起始下标和终止下标一定要是整形数。不能是浮点型。不然会报


用sort函数对浮点数呢?

    import java.util.*;  
    import java.util.Arrays;  
    public class Main {  
        public static void main(String[] args) {  
              
            Scanner in=new Scanner(System.in);  
              
            while(in.hasNext())  
            {  
                double  num[]=new double[100];  
                  
                int n;///输出n个数  
                n=in.nextInt();  
                  
                for(int i=0;i<n;i++)  
                {  
                    num[i]=in.nextDouble();  
                      
                }  
                  
                Arrays.sort(num,0,n);///排序部分  
                  
                for(int i=0;i<n;i++)  
                {  
                    System.out.println(num[i]);  
                      
                }  
            }  
      
        }  
    }  

运行结果如下



如果一个数组初始化时已经赋值。则SORT函数可以另外一种格式

Arrays.sort(数组名); 

举个例子

    import java.util.*;  
    import java.util.Arrays;  
    public class Main {  
        public static void main(String[] args) {  
              
            Scanner in=new Scanner(System.in);  
              
          
                int num[]= {5,4,3,2,1};  
                  
                Arrays.sort(num);  
                  
                for(int i=0;i<5;i++)  
                {  
                    System.out.println(num[i]);  
                      
                }  
          
      
        }  
    }  



然而,只是单纯的降序升序排序是满足不了使用需求得。

所以,我们要研究下sort函数中cmp函数的使用方法


我们来看看cmp函数的格式

int compare(Object o1, Object o2);  


我们可以看到,传入函数的是java中的类(java中没有结构体)

这时,sort函数的格式变为

    Arrays.sort(数组名, 起始下标, 终止下标, new cmp());  

怎么自定义排序呢?

基本方法

int compare(Object o1, Object o2) 返回一个基本类型的整型
如果要按照升序排序,
则o1 小于o2,返回-1(负数),相等返回0,01大于02返回1(正数)
如果要按照降序排序
 则o1 小于o2,返回1(正数),相等返回0,01大于02返回-1(负数)


以上就是在cmp函数中的自定义排序方法。


我们来举个例子。-----输入n个数,然后降序排序。

    import java.util.Arrays;  
    import java.util.Comparator;  
    import java.util.Scanner;  
    import java.util.*;    
      
    class shu ///创建类  
    {  
        int x;  
    }  
      
    class cmp implements Comparator<shu> {  
        /* 
         * 因为上面指定了类型<he>,所以此处可以直接(he A,he B) 否则要写成(Object A,Object 
         * B),再强制转换成he类型:((he)A).x 
         */  
        public int compare(shu A, shu B) ///降序排序  
        {  
                if(A.x<B.x)  
                {  
                    return 1;  
                }  
                else if(A.x==B.x)  
                {  
                    return 0;  
                }  
                else  
                {  
                    return -1;  
                }  
      
        }  
    }  
      
    public class Main {  
        public static void main(String[] args) {  
              
            Scanner in = new Scanner(System.in);  
      
            while (in.hasNext()) {  
                  
                shu num[] = new shu[100];///创建类数组  
      
                int n;  
                n = in.nextInt();  
      
                for (int i = 0; i < n; i++) {  
                      
                    num[i]=new shu();///这个地方容易漏  
                      
                    num[i].x = in.nextInt();  
                }  
                  
                Arrays.sort(num, 0, n, new cmp());  
                  
                for (int i = 0; i < n; i++) {  
                    System.out.println(num[i].x);  
                }  
      
            }  
        }  
    }  


运行结果




为了更好地理解,我们来举一些关于java结构体排序的题目


例一:HRBUST 1095 最麻烦了   点击打开链接


    import java.util.Arrays;  
    import java.util.Comparator;  
    import java.util.Scanner;  
    import java.util.*;    
      
    class shu ///创建类  
    {  
        int acm;  
        int mon;  
        int rp;  
    }  
      
    class cmp implements Comparator<shu> {  
        public int compare(shu A, shu B) ///降序排序  
        {  
                if(A.acm==B.acm)  
                {  
                    if(A.mon==B.mon)  
                    {  
                        if(A.rp<B.rp)  
                        {  
                            return 1;  
                        }  
                        else if(A.rp==B.rp)  
                        {  
                            return 0;  
                        }  
                        else  
                        {  
                            return -1;  
                        }  
                    }  
                    else  
                    {  
                        if(A.mon<B.mon)  
                        {  
                            return 1;  
                        }  
                        else if(A.mon==B.mon)  
                        {  
                            return 0;  
                        }  
                        else  
                        {  
                            return -1;  
                        }  
                    }  
                }  
                else  
                {  
                    if(A.acm<B.acm)  
                    {  
                        return 1;  
                    }  
                    else if(A.acm==B.acm)  
                    {  
                        return 0;  
                    }  
                    else  
                    {  
                        return -1;  
                    }  
                }  
      
        }  
    }  
      
    public class Main {  
        public static void main(String[] args) {  
              
            Scanner in = new Scanner(System.in);  
      
            while (in.hasNext()) {  
                  
                shu num[] = new shu[1005];///创建类数组  
      
                int n;  
                n = in.nextInt();  
      
                for (int i = 0; i < n; i++) {  
                      
                    num[i]=new shu();///这个地方容易漏  
                      
                    num[i].acm = in.nextInt();  
                    num[i].mon = in.nextInt();  
                    num[i].rp  = in.nextInt();  
                }  
                  
                Arrays.sort(num, 0, n, new cmp());  
                  
                for (int i = 0; i < n; i++) {  
                    System.out.println(num[i].acm+" "+num[i].mon+" "+num[i].rp);  
                }  
      
            }  
        }  
    }  
    //int compare(Object o1, Object o2) 返回一个基本类型的整型  
    //如果要按照升序排序,  
    //则o1 小于o2,返回-1(负数),相等返回0,01大于02返回1(正数)  
    //如果要按照降序排序  
    // 则o1 小于o2,返回1(正数),相等返回0,01大于02返回-1(负数)  
    //  
这题还有另外一种写法。(借用的)

import java.util.*;

class mans
{
	long acm,money,rp;
}
class cmp implements Comparator<mans>
{
	public int compare(mans a,mans b)
	{
		if(a.acm==b.acm)
		{
			if(a.money==b.money)
			{
				return (int)(a.rp-b.rp);
			}
			return (int)(a.money-b.money);	
		}
		return (int)(a.acm-b.acm);
	}
}
public class Main
{

	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		Scanner scanf=new Scanner(System.in);
		mans a[]=new mans[1009];
		while(scanf.hasNext())
		{
			int n=scanf.nextInt();
			for(int i=0;i<n;i++)
			{
				a[i] = new mans();
				a[i].acm=scanf.nextLong();
				a[i].money=scanf.nextLong();
				a[i].rp=scanf.nextLong();
			}
			Arrays.sort(a,0,n,new cmp());
			for(int i=n-1;i>=0;i--)
			{
				System.out.println(a[i].acm+" "+a[i].money+" "+a[i].rp);
			}
		}
	}
}



但是更多的题目中,我们会遇到字典序排序的情况

这里,我们再举一个例子

例2:HRBUST 1023 JiaoZhu and C  点击打开链接

这个代码提交会超时!!!!!!!!!!!!!


    import java.util.Arrays;  
    import java.util.Comparator;  
    import java.util.Scanner;  
    import java.util.*;    
      
    class shu ///创建类  
    {  
        String name;///比较时用String  
        int mon;  
        int hunt;  
    }  
      
    class cmp implements Comparator<shu> {  
        public int compare(shu A, shu B)   
        {  
                if(A.hunt==B.hunt)  
                {  
                    if(A.mon==B.mon)  
                    {  
                        int flag=(A.name).compareTo(B.name);///按字典序排序  
                        if(flag==0)  
                        {  
                            return 0;  
                        }  
                        else if(flag<0)  
                        {  
                            return -1;  
                        }  
                        else  
                        {  
                            return 1;  
                        }  
                    }  
                    else  
                    {  
                        if(A.mon==B.mon)  
                        {  
                            return 0;  
      
                        }  
                        else if(A.mon<B.mon)  
                        {  
                            return -1;  
                        }  
                        else  
                        {  
                            return 1;  
                        }  
                              
                    }  
                }  
                else  
                {  
      
                    if(A.hunt==B.hunt)  
                    {  
                        return 0;  
      
                    }  
                    else if(A.hunt<B.hunt)  
                    {  
                        return 1;  
                    }  
                    else  
                    {  
                        return -1;  
                    }  
                }  
      
                  
        }  
      
      
    }  
      
    public class Main {  
        public static void main(String[] args) {  
              
            Scanner in = new Scanner(System.in);  
      
            while (in.hasNext()) {  
                  
                shu num[] = new shu[100005];///创建类数组  
      
                int n;  
                n = in.nextInt();  
      
                for (int i = 0; i < n; i++) {  
                      
                    num[i]=new shu();///这个地方容易漏  
                      
                    num[i].name=in.next();  
                    num[i].hunt=in.nextInt();  
                    num[i].mon=in.nextInt();  
                }  
                  
                Arrays.sort(num, 0, n, new cmp());  
                  
                for (int i = 0; i < n; i++) {  
                    System.out.println(num[i].name);  
                }  
      
            }  
        }  
    }  


大约就是酱紫了




  • 48
    点赞
  • 136
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值