Jack Nutkins
Jack Nutkins

Reputation: 1555

Sorting Array of Objects alphabetically based on a string attribute

I have a UITableView which is populated with an array of objects of type 'Product' which each have a 'name' attribute of type string. When a cell is clicked, a modal view is shown with more information on that product.

What I would like to do is split the products up into sections depending on the first letter of their name attribute. (Like iPhone contacts).

I have been struggling with this for a while now, and I can't seem to find anything online that solves my particular problem..

Could someone point me in the right direction?

Upvotes: 2

Views: 7701

Answers (2)

Meet Doshi
Meet Doshi

Reputation: 4259

Use NSSortDescriptor like this..

NSSortDescriptor *sortDescriptor =
    [NSSortDescriptor sortDescriptorWithKey:@"name"
                                  ascending:YES
                                   selector:@selector(caseInsensitiveCompare:)];
NSArray *sortedArray = [nameOfArray sortedArrayUsingDescriptors:@[sortDescriptor]];

Hope, this is what you're looking for. Any concern get back to me. :)

Upvotes: 3

dtuckernet
dtuckernet

Reputation: 7895

There are several good examples here on SO. Here is a good example:

How to sort an NSMutableArray with custom objects in it?

Upvotes: 5

Related Questions