与Lambda表达式Java相比时出错

时间:2015-07-05 05:20:21

标签: java lambda compare

以下代码未正确排序数组。

public void sortByQuantity() {
    Arrays.sort(sortedByQuantity, (Item i1, Item i2) -> Double.compare(i1.getQuantity(), i1.getQuantity()));
}
public void sortByPrice() {
    Arrays.sort(sortedByPrice, (Item i1, Item i2) -> Double.compare(i1.getPrice(), i2.getPrice()));
}

我尝试使用lambda表达式,虽然没有控制台错误,但不进行排序 项目类别:

public class Item {
    private String name;
    private int quantity;
    private double price;

    public void setName (String name) {
        this.name = name;
    }
    public String getName () {
        return this.name;
    }
    public void setQuantity (int quantity) {
        this.quantity = quantity;
    }
    public int getQuantity () {
        return this.quantity;
    }
    public void setPrice (double price) {
        this.price = price;
    }
    public double getPrice () {
        return this.price;
    }
}

1 个答案:

答案 0 :(得分:2)

除非你复制并粘贴错误,否则请更改:

Double.compare(i1.getQuantity(), i1.getQuantity())

对此:

Double.compare(i1.getQuantity(), i2.getQuantity())