对自定义类型数据的排序总结

目录

自定义数据类型放在数组中

Arrays.sort()方法

普通版代码

优化版代码

高级版代码

自定义数据类型放在集合中

总结


自定义数据类型放在数组中

Arrays.sort()方法

这种方法要用Arrays.sort()方法,方法完整表述:

static <T> void sort( T[] a, Comparator<? super T> c)  根据指定比较器产生的顺序对指定对象数组进行排序。

解释:T[] a:自定义数据类型的数组表示(a这个数组中的每一个元素都是自定义的数据类型,比如都是People类的对象);

   Comparator<? super T> c:Comparator的一个实现类对象,可以是Comparator的一个子类对象。

完整演示代码如下:

普通版代码

中规中矩的按照JDK的要求进行写,比较麻烦。

Person类:

public class Person implements Comparator<Person> {
    private String name;
    private int age;

    public Person(){
    }

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


    @Override
    public String toString() {
        return "姓名:"+name+"\t年龄:"+age+";";
    }


    /**
     * 要实现排序功能就必须要重写Comparator接口中的compare方法
     * @param o1
     * @param o2
     * @return
     */
    @Override
    public int compare(Person o1, Person o2) {
        return o1.age- o2.age;
    }
}

实现的主类:

/**
 * 需求:
 * 使用数组存储多个Person对象
 * 对数组中的Person对象使用Arrays的sort方法通过年龄进行升序排序
 *
 */
public class Main {
    public static void main(String[] args) {

        Person[] arr=new Person[]{
                new Person("陆俊",16),
                new Person("何凡",20),
                new Person("奥特曼",100),
                new Person("迪迦",10),
        };

        System.out.println("排序前的数组中的数据:");
        for (Person per : arr) {
            System.out.println(per);
        }

        Arrays.sort(arr,new Person());

        //这样写也是可以的
       // Arrays.sort(arr,arr[0]);

        System.out.println("排序后的数组中的数据:");
        for (Person per : arr) {
            System.out.println(per);
        }
    }
}

运行结果:

 


优化版代码

使用了匿名内部类进行改写优化:

Person类:

public class Person {
    public String name;
    public int age;

    public Person(){
    }

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


    @Override
    public String toString() {
        return "姓名:"+name+"\t年龄:"+age+";";
    }
}

运行主类:

/**
 * 需求:
 * 使用数组存储多个Person对象
 * 对数组中的Person对象使用Arrays的sort方法通过年龄进行升序排序
 *
 * 使用匿名内部类改写!
 */
public class Main {
    public static void main(String[] args) {

        Person[] arr=new Person[]{
                new Person("陆俊",16),
                new Person("何凡",20),
                new Person("奥特曼",100),
                new Person("迪迦",10),
        };

        System.out.println("排序前的数组中的数据:");
        for (Person per : arr) {
            System.out.println(per);
        }

        Arrays.sort(arr, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.age- o2.age;
            }
        });

        //这样写也是可以的
       // Arrays.sort(arr,arr[0]);

        System.out.println("排序后的数组中的数据:");
        for (Person per : arr) {
            System.out.println(per);
        }
    }
}

运行结果都是一样的,所以这里就不放图了哈!


高级版代码

使用Lambda表达式进行优化:

Person类和高级版的是一样的,这里就不重复了哈!

运行主类:

/**
 * 需求:
 * 使用数组存储多个Person对象
 * 对数组中的Person对象使用Arrays的sort方法通过年龄进行升序排序
 *
 * 使用Lambda优化改写!
 */
public class Main {
    public static void main(String[] args) {

        Person[] arr=new Person[]{
                new Person("陆俊",16),
                new Person("何凡",20),
                new Person("奥特曼",100),
                new Person("迪迦",10),
        };

        System.out.println("排序前的数组中的数据:");
        for (Person per : arr) {
            System.out.println(per);
        }

        Arrays.sort(arr,(o1, o2) -> o1.age- o2.age);

        //这样写也是可以的
       // Arrays.sort(arr,arr[0]);

        System.out.println("排序后的数组中的数据:");
        for (Person per : arr) {
            System.out.println(per);
        }
    }
}

自定义数据类型放在集合中

自定义数据类型放在数组中的三种写法都是一样的,大同小异,这里就不多赘述了!(这里只演示一种写法)

Peolple类:

/**
 * Collections.sort对People进行默认的升序排序
 */
public class People implements Comparable<People>{
    private String name;
    private int age;

    public People() {
    }

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

    @Override
    public String toString() {
        return "姓名:"+name+"\t年龄:"+age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int compareTo(People o) {
        return this.age-o.age;
    }
}

运行主类:

/**
 * 演示Collections工具类中的sort方法
 */
public class Main {
    public static void main(String[] args) {

        ArrayList<People> arr=new ArrayList<>();

        arr.add(new People("迪迦",100));
        arr.add(new People("赛罗",400));
        arr.add(new People("人类",70));
        arr.add(new People("外星人",20));

        System.out.println("排序前的数组中的数据:");
        for (People per : arr) {
            System.out.println(per);
        }

        Collections.sort(arr);

        System.out.println("排序后的数组中的数据:");
        for (People per : arr) {
            System.out.println(per);
        }
    }
}

运行截图:

 


总结

前者使用了Arrays工具类的方法,后者使用Collections工具类的方法!虽然两者要实现的接口和方法都不一样,但使用Lambda表达式就感觉是一样的了,也就是第三种写法。

Arrays.sort()方法中第二个参数要传入一个Comparator接口的实现类对象,这就必须要实现接口中的compare方法;

Collections.sort()方法中第二个参数要传入一个Comparable接口的实现类对象,这就必须要实现接口中的compareTo方法。

发现没有,两个长的炒鸡像,所以直接用Lambda表达式就不用区分了!哈哈!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值