NSArray 排序得益于另一个 NSArray
我有一个包含一些对象的 NSArray。
NSArray *firstArray = [NSArray arrayWithObjects:obj, plop, color, shape, nil];
现在我有另一个 NSArray,仅包含第一个 NSArray 中的对象,但顺序不同。
NSArray *secondArray = [NSArray arrayWithObjects: shape, color, plop, nil];
我想按照与第一个数组相同的顺序对第二个数组进行排序。
我希望第二个数组是:
- 扑通
- 颜色
- 形状
I have a NSArray containing some objects.
NSArray *firstArray = [NSArray arrayWithObjects:obj, plop, color, shape, nil];
Now I have another NSArray containing only objects from the first NSArray, but not in the same order.
NSArray *secondArray = [NSArray arrayWithObjects: shape, color, plop, nil];
I would like to sort the secondArray in the same order that in the firstArray.
I want secondArray to be :
- plop
- color
- shape
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSArray 已经排序,因此您可以根据自己的优势使用它:为什么不搜索第一个数组内但不在第二个数组内的对象,然后从第一个数组中删除 then 呢?
NSArray is already ordered so you can use it at your advantage: why just don't you search what objects are inside the firstArray but not inside the secondArray then remove then from the first array ?
通常情况下,我会自动将人们指向 Apple 的 NSSortDescriptor 类,但听起来你正在做的事情非常应用程序和应用程序。对象特定。
如果您的形状、颜色和 plop 对象是真正的 Cocoa 对象(即 它们响应
isKindOfClass
选择器),您可以遍历secondArray
的每一部分并创建一个新的可变数组(我们称之为thirdArray
为参数起见),并使用secondArray
元素的枚举以您要查找的格式将每个对象插入到thirdArray
中。Normally this is the kind of thing for which I'd automatically point people to Apple's NSSortDescriptor Class, but it sounds like what you are doing is very application & object specific.
If your shape, color and plop objects are true Cocoa objects (i.e. they respond to
isKindOfClass
selectors), you can go through each piece of yoursecondArray
and create a new mutable array (let's call itthirdArray
for argument's sake) and use enumeration on the elements ofsecondArray
to insert each object intothirdArray
in the format that you are looking for.