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

Ios 如何按价格值对SKProduct数组排序,ios,nsarray,in-app-purchase,sorting,storekit,Ios,Nsarray,In App Purchase,Sorting,Storekit,在发送产品id列表后,我试图对从Apple Server收到的SKProduct数组进行排序 我只想使用以下方法进行排序: NSSortDescriptor *lowestPriceToHighest = [NSSortDescriptor sortDescriptorWithKey:@"self.price" ascending:YES]; NSArray *sortedProducts = [products sortedArrayUsingDescriptors:[NSArray arra

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

我只想使用以下方法进行排序:

NSSortDescriptor *lowestPriceToHighest = [NSSortDescriptor sortDescriptorWithKey:@"self.price" ascending:YES];
NSArray *sortedProducts = [products sortedArrayUsingDescriptors:[NSArray arrayWithObject:lowestPriceToHighest]];
但我得到了错误消息:无法访问变量lowestPriceToHighest 我的排序描述符是否定义错误

试试看:

NSArray *products = [response.products sortedArrayUsingComparator:^(id a, id b) {
        NSDecimalNumber *first = [(SKProduct*)a price];
        NSDecimalNumber *second = [(SKProduct*)b price];
        return [first compare:second];
    }];
从这个答案中:

尝试:

从这个答案中:

试着摆脱自我

NSSortDescriptor *lowestPriceToHighest = [NSSortDescriptor sortDescriptorWithKey:@"price" ascending:YES];
    NSArray *sortedProducts = [products sortedArrayUsingDescriptors:[NSArray arrayWithObject:lowestPriceToHighest]];
试着摆脱自我

NSSortDescriptor *lowestPriceToHighest = [NSSortDescriptor sortDescriptorWithKey:@"price" ascending:YES];
    NSArray *sortedProducts = [products sortedArrayUsingDescriptors:[NSArray arrayWithObject:lowestPriceToHighest]];
试试这个选择器

尝试使用此选择器

假设您有var产品:[SKProduct]您可以执行以下操作:

products.sortInPlace {
    return Float($0.price) < Float($1.price)
}
假设您有var产品:[SKProduct],您可以:

products.sortInPlace {
    return Float($0.price) < Float($1.price)
}
Swift 4:

products.sort(by: { (p0, p1) -> Bool in
    return p0.price.floatValue < p1.price.floatValue
}) 
Swift 4:

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