简体   繁体   English

基于字符串属性按字母顺序对对象数组进行排序

[英]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. 我有一个UITableView,它填充了一个'Product'类型的对象数组,每个对象都有一个string类型的'name'属性。 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). (喜欢iPhone联系人)。

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? 如何使用自定义对象对NSMutableArray进行排序?

Use NSSortDescriptor like this.. 像这样使用NSSortDescriptor ..

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. 任何关注都会回复给我。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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