
正文
java中对List中的元素进行排序
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Collections对List集合中的数据进行排序
有时候需要对集合中的元素按照一定的规则进行排序,这就需要用到
Java中提供的对集合进行操作的工具类Collections,其中的sort方法
No1.先看一个简单的例子:
public static void main(String[] args) {
List<Integer> nums = new ArrayList<Integer>();
nums.add(2);
nums.add(9);
nums.add(7);
nums.add(1);
nums.add(0);
System.out.println(nums);
Collections.sort(nums);
System.out.println(nums);
}
结果如下:
[2, 9, 7, 1, 0]
[0, 1, 2, 7, 9]
建立一个No2代码要用的entity,如下:
import java.util.Date;
public class Record implements Comparable<Record> {
private String name;
private int age;
private Date start;
public Record(String name, int age, Date start) {
this.name = name;
this.age = age;
this.start = start;
}
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;
}
public Date getStart() {
return start;
}
public void setStart(Date start) {
this.start = start;
}
@Override
public int compareTo(Record o) {
int nameIndex = this.name.compareTo(o.name);
int ageIndex = 0;
int startIndex = 0;
// 姓名一样则比较年龄
if (nameIndex == 0) {
ageIndex = this.age - o.age;
}
// 年龄一样则比较开始时间
if (ageIndex == 0) {
boolean isAfter = this.start.after(o.start);
if (isAfter) {
startIndex = 1;
} else {
startIndex = -1;
}
}
return nameIndex+ageIndex+startIndex;
}
}
No2.稍复杂一些的排序:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; public class ListOrder {
public static void main(String[] args) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
List<Record> records = new ArrayList<Record>();
records.add(new Record("a", 2, df.parse("2015-12-16")));
records.add(new Record("d", 8, df.parse("2015-12-16")));
records.add(new Record("a", 1, df.parse("2019-12-16")));
records.add(new Record("x", 0, df.parse("2014-12-16")));
records.add(new Record("a", 1, df.parse("2018-12-16")));
for (Record record : records) {
System.out.println("name :" + record.getName() + " age :" + record.getAge() + " start :" + record.getStart());
}
Collections.sort(records);
System.out.println("--------------------------------------");
for (Record record : records) {
System.out.println("name :" + record.getName() + " age :" + record.getAge() + " start :" + record.getStart());
}
}
}
输出结果:
name :a age :2 start :Wed Dec 16 00:00:00 CST 2015
name :d age :8 start :Wed Dec 16 00:00:00 CST 2015
name :a age :1 start :Mon Dec 16 00:00:00 CST 2019
name :x age :0 start :Tue Dec 16 00:00:00 CST 2014
name :a age :1 start :Sun Dec 16 00:00:00 CST 2018
--------------------------------------
name :a age :1 start :Sun Dec 16 00:00:00 CST 2018
name :a age :1 start :Mon Dec 16 00:00:00 CST 2019
name :a age :2 start :Wed Dec 16 00:00:00 CST 2015
name :d age :8 start :Wed Dec 16 00:00:00 CST 2015
name :x age :0 start :Tue Dec 16 00:00:00 CST 2014
Collections提供的第二种排序方法sort(List<T> list, Comparator<? super T> c)
所使用的entity:
import java.util.Date;
public class Record {
private String name;
private int age;
private Date start;
public Record(String name, int age, Date start) {
this.name = name;
this.age = age;
this.start = start;
}
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;
}
public Date getStart() {
return start;
}
public void setStart(Date start) {
this.start = start;
}
}
No3:代码:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; public class ListOrder { public static void main(String[] args) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
List<Record> records = new ArrayList<Record>();
records.add(new Record("a", 2, df.parse("2015-12-16")));
records.add(new Record("d", 8, df.parse("2015-12-16")));
records.add(new Record("a", 1, df.parse("2019-12-16")));
records.add(new Record("x", 0, df.parse("2014-12-16")));
records.add(new Record("a", 1, df.parse("2018-12-16")));
for (Record record : records) {
System.out
.println("name :" + record.getName() + " age :" + record.getAge() + " start :" + record.getStart());
}
Collections.sort(records, new Comparator<Record>() {
@Override
public int compare(Record r1, Record r2) {
int nameIndex = r1.getName().compareTo(r2.getName());
int ageIndex = 0;
int startIndex = 0;
// 姓名一样则比较年龄
if (nameIndex == 0) {
ageIndex = r1.getAge() - r2.getAge();
}
// 年龄一样则比较开始时间
if (ageIndex == 0) {
boolean isAfter = r1.getStart().after(r2.getStart());
if (isAfter) {
startIndex = 1;
} else {
startIndex = -1;
}
}
return nameIndex + ageIndex + startIndex;
}
});
System.out.println("--------------------------------------");
for (Record record : records) {
System.out
.println("name :" + record.getName() + " age :" + record.getAge() + " start :" + record.getStart());
}
}
}
結果:
name :a age :2 start :Wed Dec 16 00:00:00 CST 2015
name :d age :8 start :Wed Dec 16 00:00:00 CST 2015
name :a age :1 start :Mon Dec 16 00:00:00 CST 2019
name :x age :0 start :Tue Dec 16 00:00:00 CST 2014
name :a age :1 start :Sun Dec 16 00:00:00 CST 2018
--------------------------------------
name :a age :1 start :Sun Dec 16 00:00:00 CST 2018
name :a age :1 start :Mon Dec 16 00:00:00 CST 2019
name :a age :2 start :Wed Dec 16 00:00:00 CST 2015
name :d age :8 start :Wed Dec 16 00:00:00 CST 2015
name :x age :0 start :Tue Dec 16 00:00:00 CST 2014
reference:
https://blog.csdn.net/veryisjava/article/details/51675036
https://www.jb51.net/article/72284.htm
https://blog.csdn.net/u012901117/article/details/76853113






