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 
    }
}

java中数组的排序函数 java用数组排序_自定义排序

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

java中数组的排序函数 java用数组排序_数组_02

对类的自定义排序

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

java中数组的排序函数 java用数组排序_开发语言_03