0

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 Answer 1

1

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];
Sign up to request clarification or add additional context in comments.

1 Comment

What about sorting via [dog type] which is a NSString? Would this method work for strings as well, or just numbers?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.