Java中的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]);    

            }    
        }    

    }    
}    

注意:这里的起始下标和终止下标一定要是整形数。不能是浮点型。
亦可以给浮点型排序
例:

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()的第二种格式

如果一个数组初始化时已经赋值。则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) 返回一个基本类型的整型

(1)如果要按照升序排序,
则o1 小于o2,返回-1(负数),相等返回0,01大于02返回1(正数)

(2)如果要按照降序排序
则o1 小于o2,返回1(正数),相等返回0,01大于02返回-1(负数)

例:(降序)

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

class shu ///创建类    
{    
    int x;    
}    

class cmp implements Comparator<shu> {    
    /*  
     * 因为上面指定了类型<shu>,所以此处可以直接(shu A,shu B) 否则要写成(Object A,Object  
     * B),再强制转换成shu类型:((shu)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);    
            }    

        }    
    }    
}    
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.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);    
            }    

        }    
    }    
}    
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值