简体   繁体   中英

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?

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

How to sort an NSMutableArray with custom objects in it?

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. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM