Java -- 物件如何根據某個欄位排序
阿新 • • 發佈:2019-02-03
package com.redhorse.bean;
import com.redhorse.util.DateUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class FundTradeDetailBean implements java.io.Serializable, Comparable<FundTradeDetailBean> {
private static final long serialVersionUID = 1L;
private int id;
private int fundId;
private String login;
private long order;
private String symbol;
private double openPrice;
private double closePrice;
private long profit;
private long volume;
private Date openTime;
private Date closeTime;
private int closeDay;
private String comment;
private long commission;
private long agentCommission;
private int cmd;
private double sl;
private double tp;
private long swap;
private String fundName;
public String getFundName() {
return fundName;
}
public void setFundName (String fundName) {
this.fundName = fundName;
}
public FundTradeDetailBean() {
super();
}
public FundTradeDetailBean(int id, int fundId, String login, long order, String symbol, double openPrice, double closePrice, long profit, long volume, Date openTime, Date closeTime, int closeDay, String comment, long commission, long agentCommission, int cmd, double sl, double tp, long swap) {
this.id = id;
this.fundId = fundId;
this.login = login;
this.order = order;
this.symbol = symbol;
this.openPrice = openPrice;
this.closePrice = closePrice;
this.profit = profit;
this.volume = volume;
this.openTime = openTime;
this.closeTime = closeTime;
this.closeDay = closeDay;
this.comment = comment;
this.commission = commission;
this.agentCommission = agentCommission;
this.cmd = cmd;
this.sl = sl;
this.tp = tp;
this.swap = swap;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setFundId(int fundId) {
this.fundId = fundId;
}
public int getFundId() {
return fundId;
}
public void setLogin(String login) {
this.login = login;
}
public String getLogin() {
return login;
}
public void setOrder(long order) {
this.order = order;
}
public long getOrder() {
return order;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public String getSymbol() {
return symbol;
}
public void setOpenPrice(double openPrice) {
this.openPrice = openPrice;
}
public double getOpenPrice() {
return openPrice;
}
public void setClosePrice(double closePrice) {
this.closePrice = closePrice;
}
public double getClosePrice() {
return closePrice;
}
public void setProfit(long profit) {
this.profit = profit;
}
public long getProfit() {
return profit;
}
public void setVolume(long volume) {
this.volume = volume;
}
public long getVolume() {
return volume;
}
public void setOpenTime(Date openTime) {
this.openTime = openTime;
}
public Date getOpenTime() {
return openTime;
}
public void setCloseTime(Date closeTime) {
this.closeTime = closeTime;
}
public Date getCloseTime() {
return closeTime;
}
public void setCloseDay(int closeDay) {
this.closeDay = closeDay;
}
public int getCloseDay() {
return closeDay;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getComment() {
return comment;
}
public void setCommission(long commission) {
this.commission = commission;
}
public long getCommission() {
return commission;
}
public void setAgentCommission(long agentCommission) {
this.agentCommission = agentCommission;
}
public long getAgentCommission() {
return agentCommission;
}
public void setCmd(int cmd) {
this.cmd = cmd;
}
public int getCmd() {
return cmd;
}
public void setSl(double sl) {
this.sl = sl;
}
public double getSl() {
return sl;
}
public void setTp(double tp) {
this.tp = tp;
}
public double getTp() {
return tp;
}
public void setSwap(long swap) {
this.swap = swap;
}
public long getSwap() {
return swap;
}
@Override
public int compareTo(FundTradeDetailBean o) {
if (o.getOpenTime().compareTo(this.openTime) == 0) {
return 0;
} else if (o.getOpenTime().compareTo(this.openTime) > 0) {
return 1;
} else {
return -1;
}
}
public static void main(String[] args) {
FundTradeDetailBean a = new FundTradeDetailBean();
FundTradeDetailBean b = new FundTradeDetailBean();
FundTradeDetailBean c = new FundTradeDetailBean();
FundTradeDetailBean d = new FundTradeDetailBean();
a.setOpenTime(new Date());
b.setOpenTime(DateUtil.String2Date("2018-01-01"));
c.setOpenTime(DateUtil.String2Date("2018-01-02"));
d.setOpenTime(DateUtil.String2Date("2017-01-02"));
List<FundTradeDetailBean> list = new ArrayList<FundTradeDetailBean>();
list.add(a);
list.add(b);
list.add(c);
list.add(d);
Collections.sort(list);
for (FundTradeDetailBean f : list) {
System.out.println(DateUtil.Date2String(f.getOpenTime()));
}
}
}