Java 中对集合排序

Java 中对集合排序: 本人主要涉及List  

1. java 中集合的 都是 继承自  Colllection   所以 对集合排序 可以使用  Collections.Sort() 方法 

2.  通过List<E> 泛型中 E的某个属性 排序  

import java.util.Collections;
import java.util.Comparator;

import java.util.List;

// 前置代码就略过了,自行脑补吧
List<EventUserParticipation> participationList = super.getList();

// 排序
Collections.sort(participationList, new Comparator<EventUserParticipation>(){
        @Override
	public int compare(EventUserParticipation o1, EventUserParticipation o2) {
	  // TODO Auto-generated method stub
	return (int)(o2.getDateAdded().getTime() - o1.getDateAdded().getTime());
	}
					
});    

  理解:  a. 对participationList 进行排序  b. 通过 添加时间倒叙方式  排序  c.扩展   o1 - o2  为正序  

  以上代码执行完   participationList 就是排序过的集合 

     以下是我 实体 EventUserParticipation

public class EventUserParticipation{
    private Integer id;
    private Integer eventId;
    private Integer wechatUserId;
    private String drawCode;
    private Date dateAdded;
    private String uploadPic;
    private String shopId;
    private Integer customerId;

     //setter and getter 略
}    

 

posted on 2018-10-24 16:56  zy433125  阅读(177)  评论(0编辑  收藏  举报