您现在的位置是:首页 > 编程 > 

Java如何给数组排序Comparator方式

2025-07-21 01:37:50
Java如何给数组排序Comparator方式 Java如何给数组排序简介:本文致力于以实践为主的快速入门学会使用Java的排序功能,看完就会用。对常见类型的排序1.Arrays.sort() 默认排序代码语言:javascript代码运行次数:0运行复制import java.util.Arrays; public class Main { public static void mai

Java如何给数组排序Comparator方式

Java如何给数组排序

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

对常见类型的排序

1.Arrays.sort() 默认排序
代码语言:javascript代码运行次数:0运行复制
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        // 对于普通数组的排序
        Integer [] a= new Integer [] {1, -4, 0, 1, 5, , 8, 1, -100, 8};
        // sort函数默认按照字符的编码大小排序
        Arrays.sort(a);
        for (int i = 0; i < a.length; i++) {
            print(a[i] + " ");
        }
        // -100 -4 0 1 1 1  5 8 8 
    }
}
2.使用Comparator接口自定义 Arrays.sort()逆序排序
代码语言:javascript代码运行次数:0运行复制
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, , 8, 1, -100, 8};

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

        Arrays.sort(a, cmp);
        for (int i = 0; i < a.length; i++) {
            print(a[i] + " ");
        }
        // 8 8 5  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;
        }
    }
}

对类的自定义排序

代码语言:javascript代码运行次数:0运行复制
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;
             = 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[] = 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++) {
            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;
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2022-10-16,如有侵权请联系 cloudcommunity@tencent 删除接口排序数组javacomparator

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/biancheng/1160342.html

相关标签:无
上传时间: 2025-07-20 13:31:50
留言与评论(共有 10 条评论)
本站网友 迪拜黄金车
24分钟前 发表
看完就会用
本站网友 心一跳爱就开始煎熬
23分钟前 发表
0
本站网友 三亚论坛
29分钟前 发表
8}; // sort函数默认按照字符的编码大小排序 Arrays.sort(a); for (int i = 0; i < a.length; i++) { print(a[i] + " "); } // -100 -4 0 1 1 1 5 8 8 } }2.使用Comparator接口自定义 Arrays.sort()逆序排序代码语言:javascript代码运行次数:0运行复制import java.util.Arrays; import java.util.Comparator; public class Main { public static void main(String[] args) { // 对于普通数组的排序 Integer [] a= new Integer [] {1
本站网友 雍景四季二手房
1分钟前 发表
看完就会用
本站网友 点点博客
13分钟前 发表
Student o2) { int num = 0; if (o1.age >= o2.age) num = 1; else num = -1; return num; } } }本文参与 腾讯云自媒体同步曝光计划
本站网友 hdtv下载
20分钟前 发表
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; } } }对类的自定义排序代码语言:javascript代码运行次数:0运行复制import java.util.Arrays; import java.util.Comparator; public class Main { static class Student{ public int age; public String name; public Student(int age
本站网友 乌云盖顶
10分钟前 发表
8
本站网友 北京肿瘤医院挂号
23分钟前 发表
5
本站网友 卖商铺
19分钟前 发表
5