如何在Java中对方法列表进行排序?

时间:2019-01-17 14:40:46

标签: java list sorting

在下面,您将看到我的代码摘要。我需要对getTopBids进行排序,以使最高出价排在第一,其次是第二高,以此类推。...我该怎么做? Java的新手-这是我在学校的第一个作业!

   public String getTopBids(){
    StringBuilder topBids = new StringBuilder();
    ArrayList<String> topBidsName = new ArrayList<>();
    ArrayList<Integer> topBidsAmount = new ArrayList<>();
    for(int i = 0; i < 3; i++) {
        String name = "";
        int amount = 0;

        for (Bid bid : this.getBidding()) {
            if(amount < bid.getAmount() && !topBidsAmount.contains(bid.getAmount())) {
                name = bid.getUser().getName();
                amount = bid.getAmount();
            }
        }
        if(amount != 0){
            topBidsName.add(name);
            topBidsAmount.add(amount);
        }
    }
    for (int i = 0; i < topBidsName.size(); i++){
        topBids.append(String.format("%s %d kr", topBidsName.get(i),
            topBidsAmount.get(i)));
        if(i != topBidsName.size()) topBids.append(", ");
    }
    return topBids.toString(); }

}

1 个答案:

答案 0 :(得分:1)

使用

Collections.sort(topBidsAmount);

另请参阅How to use Collections.sort() in Java?