Java 合并两个list并按照某个字段排序

Posted 牧の风

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 合并两个list并按照某个字段排序相关的知识,希望对你有一定的参考价值。

public static void main(String[] args) {
        List<ReplyInfo> replyInfos1 = new ArrayList<>();
        ReplyInfo r1 = new ReplyInfo();
        r1.setInsertTime(1544326153000L);//12-9
        r1.setAlias("r1");
        ReplyInfo r2 = new ReplyInfo();
        r2.setInsertTime(1544585353000L);//12-12
        r2.setAlias("r2");

        replyInfos1.add(r1);
        replyInfos1.add(r2);

        List<ReplyInfo> replyInfos2 = new ArrayList<>();

        ReplyInfo r3 = new ReplyInfo();
        r3.setInsertTime(1544498953000L);//12-11
        r3.setAlias("r3");
        ReplyInfo r4 = new ReplyInfo();
        r4.setInsertTime(1544412553000L);//12-10
        r4.setAlias("r4");

        ReplyInfo r5 = new ReplyInfo();
        r5.setInsertTime(1544412553000L);//12-10
        r5.setAlias("r5");

        replyInfos2.add(r3);
        replyInfos2.add(r4);
        replyInfos2.add(r5);
        List<ReplyInfo> compareList = new ArrayList<>();
        compareList.addAll(replyInfos1);
        compareList.addAll(replyInfos2);
        Collections.sort(compareList,new Comparator<ReplyInfo>(){
            @Override
            public int compare(ReplyInfo arg0, ReplyInfo arg1) {
                int count = (int) ( arg1.getInsertTime()-arg0.getInsertTime());
                if (count == 0){
                    return 0;
                }
                if (count > 0){
                    return 1;
                }

                return  -1 ;
            };
        });

        for(ReplyInfo stu : compareList){
            System.out.println("alias------>:"+stu.getAlias()+"insertTime------->"+stu.getInsertTime());
        }
    }

  最后 如果两个时间相同还要根据 具体内容排序 可以count= 0时继续排序

 

ReplyInfo 对象:
package com.community.entrance.entity.blog;

import java.io.Serializable;

/**
 * 回复bean
 *
 * @author m.feng
 * @create 2018-12-07-10:15
 */
public class ReplyInfo implements Serializable {

    private Integer id;

    private long replyId;

    private long commentId;

    private long blogId;

    private long beReplyId;

    private String replyType;

    private String replyContent;

    private Integer fromUid;

    private Integer toUid;

    private Integer isPermit;

    private long insertTime;

    private Integer status;

    private String headThumb;

    private String alias;

    private long bReplyId;

    private long bCommentId;

    private String bReplyContent;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public long getReplyId() {
        return replyId;
    }

    public void setReplyId(long replyId) {
        this.replyId = replyId;
    }

    public long getCommentId() {
        return commentId;
    }

    public void setCommentId(long commentId) {
        this.commentId = commentId;
    }

    public String getReplyType() {
        return replyType;
    }

    public void setReplyType(String replyType) {
        this.replyType = replyType;
    }

    public String getReplyContent() {
        return replyContent;
    }

    public void setReplyContent(String replyContent) {
        this.replyContent = replyContent;
    }

    public Integer getFromUid() {
        return fromUid;
    }

    public void setFromUid(Integer fromUid) {
        this.fromUid = fromUid;
    }

    public Integer getToUid() {
        return toUid;
    }

    public void setToUid(Integer toUid) {
        this.toUid = toUid;
    }

    public Integer getIsPermit() {
        return isPermit;
    }

    public void setIsPermit(Integer isPermit) {
        this.isPermit = isPermit;
    }

    public long getInsertTime() {
        return insertTime;
    }

    public void setInsertTime(long insertTime) {
        this.insertTime = insertTime;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getHeadThumb() {
        return headThumb;
    }

    public void setHeadThumb(String headThumb) {
        this.headThumb = headThumb;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public long getBlogId() {
        return blogId;
    }

    public void setBlogId(long blogId) {
        this.blogId = blogId;
    }

    public long getBeReplyId() {
        return beReplyId;
    }

    public void setBeReplyId(long beReplyId) {
        this.beReplyId = beReplyId;
    }

    public long getbReplyId() {
        return bReplyId;
    }

    public void setbReplyId(long bReplyId) {
        this.bReplyId = bReplyId;
    }

    public long getbCommentId() {
        return bCommentId;
    }

    public void setbCommentId(long bCommentId) {
        this.bCommentId = bCommentId;
    }

    public String getbReplyContent() {
        return bReplyContent;
    }

    public void setbReplyContent(String bReplyContent) {
        this.bReplyContent = bReplyContent;
    }

    @Override
    public String toString() {
        return "ReplyInfo{" +
                "id=" + id +
                ", replyId=" + replyId +
                ", commentId=" + commentId +
                ", blogId=" + blogId +
                ", beReplyId=" + beReplyId +
                ", replyType=‘" + replyType + ‘\‘‘ +
                ", replyContent=‘" + replyContent + ‘\‘‘ +
                ", fromUid=" + fromUid +
                ", toUid=" + toUid +
                ", isPermit=" + isPermit +
                ", insertTime=" + insertTime +
                ", status=" + status +
                ", headThumb=‘" + headThumb + ‘\‘‘ +
                ", alias=‘" + alias + ‘\‘‘ +
                ", bReplyId=" + bReplyId +
                ", bCommentId=" + bCommentId +
                ", bReplyContent=‘" + bReplyContent + ‘\‘‘ +
                ‘}‘;
    }
}

  


 

以上是关于Java 合并两个list并按照某个字段排序的主要内容,如果未能解决你的问题,请参考以下文章

两个list列表排序的问题

java 如何判断list里某一字段值全部相等

要取一个字段中按照排序后的第一条记录,SQL语句怎么写

PHP 里面的数组按照某个时间字段排序

java 中 List<T>如何按照T中的一个字段排序?

SQL 如何根据两个字段排序