求一个java List排序

发布网友 发布时间:2022-04-22 22:41

我来回答

2个回答

热心网友 时间:2023-08-28 21:22

刚好学了一下:
package com.share.tools;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class MyComparator {

public static void main(String[] args) {
// 排序方式:优先度(priority)=2,date, id,name
List<MyBean> list = new ArrayList<MyBean>();
MyBean bean = new MyBean();
bean.setAge(14);
bean.setId(1003L);
bean.setName("abd");
list.add(bean);

bean = new MyBean();
bean.setAge(13);
bean.setId(1003L);
bean.setName("abc");
bean.setPriority(1);
list.add(bean);

bean = new MyBean();
bean.setAge(13);
bean.setId(1000L);
bean.setName("abc");
list.add(bean);

bean = new MyBean();
bean.setAge(13);
bean.setId(1002L);
bean.setName("ab");
list.add(bean);

bean = new MyBean();
bean.setAge(15);
bean.setId(1003L);
bean.setName("aac");
list.add(bean);

bean = new MyBean();
bean.setAge(12);
bean.setId(1001L);
bean.setName("abcd");
bean.setPriority(2);
list.add(bean);

bean = new MyBean();
bean.setAge(13);
bean.setId(1004L);
bean.setName("abc");
bean.setPriority(2);
list.add(bean);

bean = new MyBean();
list.add(bean);

bean = new MyBean();
bean.setAge(13);
list.add(bean);

bean = new MyBean();
bean.setId(1004L);
list.add(bean);

bean = new MyBean();
bean.setName("abc");
list.add(bean);

bean = new MyBean();
bean.setId(1000L);
bean.setDate(new Date("2010/10/11"));
list.add(bean);

bean = new MyBean();
bean.setId(1007L);
bean.setDate(new Date("2010/10/10"));
list.add(bean);

System.out.println("SORT前:");
for (MyBean mb : list) {
System.out.println("********************************************");
System.out.println("**priority=" + mb.getPriority() + ", date=" + mb.getDate() + ", id=" + mb.getId() + ", name=" + mb.getName() + ", age=" + mb.getAge());
System.out.println("********************************************");
}

Collections.sort(list, new ToSort());

System.out.println("SORT后:");
for (MyBean mb : list) {
System.out.println("********************************************");
System.out.println("**priority=" + mb.getPriority() + ", date=" + mb.getDate() + ", id=" + mb.getId() + ", name=" + mb.getName() + ", age=" + mb.getAge());
System.out.println("********************************************");
}
}
}

class ToSort implements Comparator<MyBean> {

@Override
public int compare(MyBean o1, MyBean o2) {

Integer priority1 = o1.getPriority();
Integer priority2 = o2.getPriority();

if (( priority1 == null || priority1 != 2) && priority2 != null && priority2 == 2) {
return 1;
}

if (priority1 != null && priority1 == 2 && (priority2 == null || priority2 != 2)) {
return -1;
}

Date date1 = o1.getDate();
Date date2 = o2.getDate();
if (date1 == null && date2 != null) {
return -1;
}
if (date1 != null && date2 == null) {
return 1;
}
if (date1 != null && date2 != null && !date1.equals(date2)) {
return date1.compareTo(date2);
}

Long id1 = o1.getId();
Long id2 = o2.getId();
if (id1 == null && id2 != null) {
return -1;
}

if (id1 != null && id2 == null) {
return 1;
}

if (id1 != null && id2 != null && id1.longValue() != id2.longValue()) {
return id1.compareTo(id2);
}

String name1 = o1.getName();
String name2 = o2.getName();
if (name1 == null && name2 == null) {
return 0;
}

if (name1 == null) {
return -1;
}

if (name2 == null) {
return 1;
}

return name1.compareTo(name2);
}

}

class MyBean {
private Integer age;
private String name;
private Long id;
private Date date;
private Integer priority;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Integer getPriority() {
return priority;
}
public void setPriority(Integer priority) {
this.priority = priority;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}

热心网友 时间:2023-08-28 21:23

用list的迭代器逐条迭代出list内容,可以增加if条件如果满足条件,将赋值给新建的bean,然后置于顶(插入或移动),把这个bean删除

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com