gpt4 book ai didi

arrays - 按属性排列数组

转载 作者:行者123 更新时间:2023-11-28 10:56:22 25 4
gpt4 key购买 nike

我有一组这样的对象数组:

[ProductDetails(name: "Tai Tou Choy",
productDescription: "",
deliveryTime: "24 hours",
img1: "https://api.com.my/images/data/4983-img1-1463122485.jpg",
thumbImg1: "https://api.com.my/images/data/thumbs/4983-img1-1463122485.jpg",
img2: "",
thumbImg2: "",
img3: "",
thumbImg3: "",
videoLink: "",
sku: "0000000004983",
relatedProduct: "",
priceOption: [PriceOption(id: 9931,
label: "500g",
price: "4.56",
promo: "0",
quantity: 999)],
deliveryZone: [DeliveryZone(zone: "3")],
deliveryZoneName: [DeliveryZoneName(zoneName: "Within Klang Valley")],
qrCode: "4983"),
ProductDetails(name: "Penguin Asam Jawa",
productDescription: "",
deliveryTime: "24 hours",
img1: "https://api.com.my/images/data/1004-img1.jpg",
thumbImg1: "https://api.com.my/images/data/thumbs/1004-img1.jpg",
img2: "",
thumbImg2: "",
img3: "",
thumbImg3: "",
videoLink: "",
sku: "0000000001004",
relatedProduct: "",
priceOption: [PriceOption(id: 6971,
label: "1 kg",
price: "4.80",
promo: "0",
quantity: 864)],
deliveryZone: [DeliveryZone(zone: "3")],
deliveryZoneName: [DeliveryZoneName(zoneName: "Within Klang Valley")],
qrCode: "1004")]

我想从数组上方进行价格排序,PriceOption 数组也作为对象数组存储在主数组中。我如何从 PriceOption 进行排序?我尝试这样做:

    for i in self.productList{

i.priceOption.sortInPlace{

$0.price.localizedCaseInsensitiveCompare($1.price) == NSComparisonResult.OrderedDescending
}
}

返回错误“不能在不可变值上使用可变成员:‘i’是‘let’常量。”

如何做到这一点?

最佳答案

这就是我如何解决您提出的问题。我正在使用数组类的 sorted 方法。

解决方法:

self.productList.sorted{ Float($0.priceOption.first!.price)! > Float($1.priceOption.first!.price)!  }

关于arrays - 按属性排列数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43225016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com