我在Java中遇到了一些关于排序价格的问题。
我希望ArrayList
分别从高价到低价以及从低价到高价。
ArrayList<orderbook> buyerlist=new ArrayList<orderbook>();
我希望buyerlist
按价格对对象序列进行排序。我在下面试过,但它不起作用。
buyerlist.add(new orderbook(orderrecord.get(i).getSide(),orderrecord.get(i).getVolume(),orderrecord.get(i).getPrice()));
Collections.sort(arraylist);
ArrayList<orderbook> sellerlist=new ArrayList<orderbook>();
我希望卖家列表可以按价格排序对象序列
这是我的构造函数
public class orderbook {
// here i defined three object of this constructor.
// i want sorting this sequence by price. from high to low and low to high respectively.
String side;
int volume;
double price;
public orderbook(String side,int volume,double price){
this.side=side;
this.volume=volume;
// compare price is low then 0 will throw exception
if (volume < 0) {
throw new IllegalArgumentException("volume size illegal");
}
this.price=price;
if (price < 0) {
throw new IllegalArgumentException("price > 0 require");
}
}
public String getSide() {
return side;
}
public void setSide(String side) {
this.side = side;
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
答案 0 :(得分:0)
如果您在某些Collection
中拥有订单簿,例如ArrayList
,那么您可以按顺序对它们进行排序:
Collections.sort(orderBooks, new Comparator<orderbook> {
@Override
public int compare(orderbook ob1, orderbook ob2) {
return o1.getPrice() - o2.getPrice();
}
});