Hi I have an array having 100 element. and each element is of type class Data
with the following members:
- name
- price
- stock
- description
How can I sort the array on based on the price
member of class Data
?
Hi I have an array having 100 element. and each element is of type class Data
with the following members:
How can I sort the array on based on the price
member of class Data
?
NSSortDescriptor *priceDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"price" ascending:YES] autorelease]; // ascending YES or NO depends on your requirement
NSArray *sortDescriptors = [NSArray arrayWithObject: priceDescriptor];
NSArray *sortedArray = [yourArray sortedArrayUsingDescriptors:sortDescriptors];
also see this
if you have method for price the object within array class you can use NSComparisonResult
:
- (NSComparisonResult) compareprice : (id) element
{
return [price compare: [element price ]];
}