如何按价格值对SKProduct数组进行排序

tig*_*ero 1 sorting nsarray storekit in-app-purchase ios

我试图在发送产品ID列表后对从Apple Server收到的SKProduct数组进行排序.

我想通过使用以下内容进行排序:

NSSortDescriptor *lowestPriceToHighest = [NSSortDescriptor sortDescriptorWithKey:@"self.price" ascending:YES];
NSArray *sortedProducts = [products sortedArrayUsingDescriptors:[NSArray arrayWithObject:lowestPriceToHighest]];
Run Code Online (Sandbox Code Playgroud)

但我收到错误消息:Unable to access variable "lowestPriceToHighest" 我的排序描述符是否被错误地定义了?

小智 7

尝试删除"自我"

NSSortDescriptor *lowestPriceToHighest = [NSSortDescriptor sortDescriptorWithKey:@"price" ascending:YES];
    NSArray *sortedProducts = [products sortedArrayUsingDescriptors:[NSArray arrayWithObject:lowestPriceToHighest]];
Run Code Online (Sandbox Code Playgroud)


mmd*_*080 5

斯威夫特 4:

products.sort(by: { (p0, p1) -> Bool in
    return p0.price.floatValue < p1.price.floatValue
}) 
Run Code Online (Sandbox Code Playgroud)