Java如何给数组排序Comparator方式

Java如何给数组排序

简介:本文致力于以实践为主的快速入门学会使用Java的排序功能,看完就会用。

对常见类型的排序

1.Arrays.sort() 默认排序

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        // 对于普通数组的排序
        Integer [] a= new Integer [] {1, -4, 0, 1, 5, 3, 8, 1, -100, 8};
        // sort函数默认按照字符的编码大小排序
        Arrays.sort(a);
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        // -100 -4 0 1 1 1 3 5 8 8 
    }
}

请添加图片描述

2.使用Comparator接口自定义 Arrays.sort()逆序排序

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

public class Main {
    public static void main(String[] args) {
        // 对于普通数组的排序
        Integer [] a= new Integer [] {1, -4, 0, 1, 5, 3, 8, 1, -100, 8};

        // 创建自定义排序接口
        Comparator cmp = new MyComparator();

        Arrays.sort(a, cmp);
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        // 8 8 5 3 1 1 1 0 -4 -100
    }
    // 通过内部类重写Comparator接口实现 自定义排序
    static class MyComparator implements Comparator<Integer>{
        // 这里的接口的属性是泛型T实现的
        // 所以这里的T需要是一个类 对于int类型数据需要使用Integer定义
        // double类型的需要 Double
        // char类型的需要 Character
        @Override
        public int compare(Integer o1, Integer o2) {
            // 如果是升序排序的话 那么就是对于[a,b] 必有 a<=b
            // 那么当a<=b的时候就是-1代表不需要换两个元素的位置
            // 反之为1那么代表需要换两个元素的位置
            // 所以当o1 >= o2的时候为-1的时候代表元素的位置不需要变化
            // o1 < o2的时候需要换 那么就是逆序了
            int num = 0;
            if (o1 >= o2) num = -1;
            else num = 1;
            return num;
        }
    }
}

请添加图片描述

对类的自定义排序

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

public class Main {
    static class Student{
        public int age;
        public String name;

        public Student(int age, String name) {
            this.age = age;
            this.name = name;
        }

        // 重写toString方法方便显示
        @Override
        public String toString() {
            return "Student{" +
                    "age=" + age +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
    public static void main(String[] args) {
        // 对于普通数组的排序
        Student [] a= new Student [5];
        a[0] = new Student(10, "李华");
        a[1] = new Student(9, "李明");
        a[2] = new Student(12, "李肖");
        a[3] = new Student(1, "李大");
        a[4] = new Student(5, "李页");
        // 创建自定义排序接口
        Comparator cmp = new MyComparator();

        // 对Student类按照年龄升序排序
        Arrays.sort(a, cmp);
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        // Student{age=1, name='李大'} Student{age=5, name='李页'} Student{age=9, name='李明'} Student{age=10, name='李华'} Student{age=12, name='李肖'}  
    }
    // 通过内部类重写Comparator接口实现 自定义排序
    static class MyComparator implements Comparator<Student>{
    	// 以学生年龄来正序排序
        @Override
        public int compare(Student o1, Student o2) {
            int num = 0;
            if (o1.age >= o2.age) num = 1;
            else num = -1;
            return num;
        }
    }
}

请添加图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极客李华

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值