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

yb270580 单选题 共 个子题

现有商品类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;
};
正在加载题目数据…
已答 /
子题
该题型需在试卷/作业中作答,单题练习暂不支持
答案解析
讨论
发布
登录后参与讨论 立即登录
发布讨论需先绑定并验证手机号 去绑定手机号
加载中…
我
我
回复 @
回复 @
发送
还没有人讨论,来发表第一条吧