第29331题 单选题
以下哪个代码片段可以正确实现指定的多优先级商品排序需求?

现有商品类Commodity定义如下:

class Commodity {
    int stock; // 库存数量
    double price; // 商品售价
    long shelfTime; // 上架时间戳,值越小代表上架越早
}

排序规则为:1. 优先按库存数量从高到低排序;2. 库存相同时,按商品售价从低到高排序;3. 前两项均相同时,按上架时间从早到晚排序。

A

Comparator<Commodity> cmp = (a, b) -> {
    if (a.price != b.price) return (int)(a.price - b.price);
    if (a.stock != b.stock) return b.stock - a.stock;
    return a.shelfTime - b.shelfTime;
};```
B

Comparator<Commodity> cmp = (a, b) -> {
    if (a.stock != b.stock) return b.stock - a.stock;
    if (a.price != b.price) return (int)(a.price - b.price);
    return a.shelfTime - b.shelfTime;
};```
C

Comparator<Commodity> cmp = (a, b) -> {
    if (a.shelfTime != b.shelfTime) return a.shelfTime - b.shelfTime;
    if (a.price != b.price) return (int)(a.price - b.price);
    return b.stock - a.stock;
};```
D

Comparator<Commodity> cmp = (a, b) -> {
    if (a.stock != b.stock) return a.stock - b.stock;
    if (a.price != b.price) return (int)(b.price - a.price);
    return b.shelfTime - a.shelfTime;
};```
程序运行统计
暂无判题统计
提交0次 正确率0.00%
答案解析