views:

46

answers:

1

Hi,

I have an NSArray which contains information from an RSS feed on dogs, such as [dog types], [dog age] and [dog size]. At the moment my UITableView simply displays each cell on each dog and within the cell lists [dog types], [dog age] and [dog size].

I want to be able to allow users of my app to "sort" this data based on the dog name, dog size or dog age when they press a UIButton in the top nav-bar.

I'm struggling to work out how to filter the UITableView based on these factors, so any help is appreciated.

Thanks.

+1  A: 

Look into the sortedArrayUsingDescriptors: method. For example, assuming age is of a class that has a compare: method (NSNumber), this would sort your array by age.

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
NSArray *sortedByAge = [dogs sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
Ole Begemann
What about sorting via [dog type] which is a NSString? Would this method work for strings as well, or just numbers?
Graeme