按字母顺序对自定义对象的 NSMutableArray 进行排序
我有一个自定义对象的 NSMutableArray。自定义对象(标签类)有两个定义如下的属性:
@interface Tag : NSObject
@property (strong, nonatomic) NSString *tid;
@property (strong, nonatomic) NSString *name;
那么,我想按标签名称对这些标签对象的 NSMutableArray 进行排序 - 我该怎么做?
另外,我应该在加载该数组时对其进行排序,还是在每次读取其内容时对其进行排序?
I have an NSMutableArray of custom objects. The custom object (Tag Class) has an two properties defined as so:
@interface Tag : NSObject
@property (strong, nonatomic) NSString *tid;
@property (strong, nonatomic) NSString *name;
So, I want to sort my NSMutableArray of these Tag objects by the Tag name - how do I do this?
Also, should I sort this array as it gets loaded or sort it each time I read its contents?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Objective C 中,有多种方法可以对可变数组进行排序。
我发现的最简单的方法是使用 [NSMutableArray sortUsingFunction:] 方法。
对于您的示例,这样的内容应该足以满足排序函数的要求
,这里有一个完整的源代码列表,您可以使用它作为解决方案的基础:
将其分解为相关的组件:
函数
这只是一个普通的 C 函数(即不是一个方法,因此您不需要在任何特定类的实现部分中定义它,但是在它适用的类中定义它通常是有意义的) 。
在上面的示例中,我将其定义在 Tag 的 @end 之后,只是为了明确它不是类方法。
我命名为“ignore”的参数是排序的“上下文”。在这种情况下,没有上下文,因此我们将忽略它的值。
由于您是基于 NSString 属性进行排序,因此 NSString 的比较方法很方便,但是您也可以手动返回 {NSOrderedAscending、NSOrderedSame、NSOrderedDescending} 之一
调用排序
这会重复为中的每个元素调用您的函数数组,对数组进行排序。
如果你想更具体地了解排序类型,NSString 中还有许多其他方法可以进行比较,这里是 NSString.h 中的“剪切和粘贴”,如果你想研究,它应该可以帮助你开始更进一步。
在回答第二个问题时,每当添加或移动可能导致数组未排序的元素时,您都需要执行此操作。
There are several ways to sort a mutable array in objective c.
The simplest I have found is using the [NSMutableArray sortUsingFunction:] method.
For your example, something like this should suffice for the sort function
here is a full source listing, that you can use to base your solution on:
Breaking this down into it's relevant components:
The function
This is simply a plain C function (i.e. not a method, so you don't need to define it inside an implementation section of any particular class, however it usually makes sense to define it in the class it applies to).
In the example above, I have defined it after the @end for Tag, simply to make it clear that it it is not a class method.
The parameter I have named "ignore" is the "context" for the sort. In this case there is no context, so we are going to ignore it's value.
Since you are sorting based on an NSString property, NSString's compare method is convenient, however you can also manually return one of {NSOrderedAscending, NSOrderedSame, NSOrderedDescending}
Invoking the sort
This repeatedly calls your function for each element in the array, to sort the array.
If you want to be more specific about the type of sort, there are a number of other methods in NSString that can do the comparison, here is a "cut and paste" from NSString.h which should get you started if you want to research it further.
In answer to your second question, you would need to do this whenever you have added or moved an element that might cause the array to be unsorted.
我知道的最简单的方法是使用 sortDescriptor:
The easiest way I know is using a sortDescriptor: