java给某个对象集合排序

Replymessage.java implements Comparable, 实现compareTo()
import java.util.Date;


/**
 * Replymessage generated by MyEclipse - Hibernate Tools
 */

public class Replymessage  implements java.io.Serializable, Comparable {


    // Fields    

     private Integer id;
     private Message message;
     private User user;
     private String RContent;
     private Date RPublishTime;


    // Constructors

    /** default constructor */
    public Replymessage() {
    }

	/** minimal constructor */
    public Replymessage(Message message, User user) {
        this.message = message;
        this.user = user;
    }
    
    /** full constructor */
    public Replymessage(Message message, User user, String RContent, Date RPublishTime) {
        this.message = message;
        this.user = user;
        this.RContent = RContent;
        this.RPublishTime = RPublishTime;
    }

   
    // Property accessors

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

    public Message getMessage() {
        return this.message;
    }
    
    public void setMessage(Message message) {
        this.message = message;
    }

    public User getUser() {
        return this.user;
    }
    
    public void setUser(User user) {
        this.user = user;
    }

    public String getRContent() {
        return this.RContent;
    }
    
    public void setRContent(String RContent) {
        this.RContent = RContent;
    }

    public Date getRPublishTime() {
        return this.RPublishTime;
    }
    
    public void setRPublishTime(Date RPublishTime) {
        this.RPublishTime = RPublishTime;
    }

    // 
	public int compareTo(Object o) {
		Replymessage replyMessage = (Replymessage) o;
		return -(this.RPublishTime.compareTo(replyMessage.RPublishTime));
	}
}


获得ReplyMessage的一个set, 直接new TreeSet(Set),进行排序
		Set replyMessageSet = message.getReplymessages();

		// replyMessageSet need sort
		TreeSet sortedReplyMessages = new TreeSet(replyMessageSet);


你可能感兴趣的:(java,Hibernate,MyEclipse)