java 数组排序自定义_Java针对ArrayList自定义排序的2种实现方法

本文实例讲述了Java针对ArrayList自定义排序的2种实现方法。分享给大家供大家参考,具体如下:

Java中实现对list的自定义排序主要通过两种方式

1)让需要进行排序的对象的类实现Comparable接口,重写compareTo(T o)方法,在其中定义排序规则,那么就可以直接调用Collections.sort()来排序对象数组

public class Student implements Comparable{

private int id;

private int age;

private int height;

private String name;

public Student(int id, String name, int age, int height) {

this.id = id;

this.name = name;

this.age = age;

this.height = height;

}

public int getId() {

return id;

}

public int getAge() {

return age;

}

public int getHeight() {

return height;

}

public String getName() {

return name;

}

public void setId(int id) {

this.id = id;

}

public void setAge(int age) {

this.age = age;

}

public void setName(String name) {

this.name = name;

}

public void setHeight(int height) {

this.height = height;

}

@Override

public int compareTo(Object o) {

Student s = (Student) o;

if (this.age > s.age) {

return 1;

}

else if (this.age < s.age) {

return -1;

}

else {

if (this.height >= s.height) {

return 1;

}

else {

return -1;

}

}

}

}

测试类:

import java.util.*;

public class Test {

public static void printData(List list) {

for (Student student : list) {

System.out.println("学号:" + student.getId() + " 姓名:" + student.getName() + " 年龄" + student.getAge() + " 身高:" + student.getHeight());

}

}

public static void main(String[] args) {

List list = new ArrayList<>();

list.add(new Student(1, "A", 20, 180));

list.add(new Student(2, "B", 21, 175));

list.add(new Student(3, "C", 22, 190));

list.add(new Student(4, "D", 21, 170));

list.add(new Student(5, "E", 20, 185));

System.out.println("before sorted");

printData(list);

Collections.sort(list);

System.out.println("after age and height sorted");

printData(list);

}

}

结果:

before sorted

学号:1 姓名:A 年龄20 身高:180

学号:2 姓名:B 年龄21 身高:175

学号:3 姓名:C 年龄22 身高:190

学号:4 姓名:D 年龄21 身高:170

学号:5 姓名:E 年龄20 身高:185

after age and height sorted

学号:1 姓名:A 年龄20 身高:180

学号:5 姓名:E 年龄20 身高:185

学号:4 姓名:D 年龄21 身高:170

学号:2 姓名:B 年龄21 身高:175

学号:3 姓名:C 年龄22 身高:190

2)实现比较器接口Comparator,重写compare方法,直接当做参数传进sort中

public class Student {

private int id;

private int age;

private int height;

private String name;

public Student(int id, String name, int age, int height) {

this.id = id;

this.name = name;

this.age = age;

this.height = height;

}

public int getId() {

return id;

}

public int getAge() {

return age;

}

public int getHeight() {

return height;

}

public String getName() {

return name;

}

public void setId(int id) {

this.id = id;

}

public void setAge(int age) {

this.age = age;

}

public void setName(String name) {

this.name = name;

}

public void setHeight(int height) {

this.height = height;

}

}

测试类:

import java.util.*;

public class Test {

public static void printData(List list) {

for (Student student : list) {

System.out.println("学号:" + student.getId() + " 姓名:" + student.getName() + " 年龄" + student.getAge() + " 身高:" + student.getHeight());

}

}

public static void main(String[] args) {

List list = new ArrayList<>();

list.add(new Student(1, "A", 20, 180));

list.add(new Student(2, "B", 21, 175));

list.add(new Student(3, "C", 22, 190));

list.add(new Student(4, "D", 21, 170));

list.add(new Student(5, "E", 20, 185));

System.out.println("before sorted");

printData(list);

Collections.sort(list, new Comparator() {

@Override

public int compare(Student o1, Student o2) {

if(o1.getAge() >= o2.getAge()) {

return 1;

}

else {

return -1;

}

}

});

System.out.println("after age sorted");

printData(list);

Collections.sort(list, new Comparator() {

@Override

public int compare(Student o1, Student o2) {

if(o1.getAge() > o2.getAge()) {

return 1;

}

else if (o1.getAge() < o2.getAge()){

return -1;

}

else {

if (o1.getHeight() >= o2.getHeight()) {

return 1;

}

else {

return -1;

}

}

}

});

System.out.println("after age and height sorted");

printData(list);

}

}

输出结果:

before sorted

学号:1 姓名:A 年龄20 身高:180

学号:2 姓名:B 年龄21 身高:175

学号:3 姓名:C 年龄22 身高:190

学号:4 姓名:D 年龄21 身高:170

学号:5 姓名:E 年龄20 身高:185

after age sorted

学号:1 姓名:A 年龄20 身高:180

学号:5 姓名:E 年龄20 身高:185

学号:2 姓名:B 年龄21 身高:175

学号:4 姓名:D 年龄21 身高:170

学号:3 姓名:C 年龄22 身高:190

after age and height sorted

学号:1 姓名:A 年龄20 身高:180

学号:5 姓名:E 年龄20 身高:185

学号:4 姓名:D 年龄21 身高:170

学号:2 姓名:B 年龄21 身高:175

学号:3 姓名:C 年龄22 身高:190

单从上面的例子可以看出排序是稳定的,去看了下java的Collections.sort的源代码,确实是基于稳定的归并排序实现的,内部还做了优化,叫TimSort。(关于TimSort还可参考https://baike.baidu.com/item/TimSort?fr=aladdin)

PS:这里再为大家推荐一款关于排序的演示工具供大家参考:

希望本文所述对大家java程序设计有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值