使用自定义类基于其他数组对NSArray进行排序 [英] Sorting NSArray based on other Array with custom classes

查看:75
本文介绍了使用自定义类基于其他数组对NSArray进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我迫切需要对数组进行排序:这就是这种情况,

I desperately need to sort an array: heres the situation,

我需要根据另一个数组中的另一个对象类重新排列/排序和替换数组.

I need to rearrange / sort and replace arrays based on other an object class from another array.

ParentClass :NSObject{
  NSString *name;
  NSNumber *type;
}

这是父类,它填充了parentArray

This is the parent class, which fills up the parentArray

parentArray = { parentA, parentB, parentC }

这是另一个对象类,它具有ParentClass * parent.

This is another object class, it has ParentClass *parent.

@class ParentClass;

EntryClass: NSObject{
 NSString *name;
 NSNumber *number;
 ParentClass *parent;
}



Now I have an array entryArray = { entryX, entryY, entryZ };

entryX.parent is one of the ParentClasses and all of them are in parentArray.

entryX.parent  == ParentC;
entryY.parent  == ParentA;
entryZ.parent  == ParentB;

我想根据parentArray中的顺序对这个entryArray进行排序. 因此,如果ParentArray为:

I want to sort this entryArray, according the sequence in parentArray. So if ParentArray is:

{
 ParentA,
 ParentB,
 ParentC
}

然后我希望排序后的entryArray为

then I'd want the sorted entryArray to be

{
 entryY,
 entryZ,
 entryX
}

此外,如果缺少一个条目Object(条目Z),则该数组应该为

Also, if theres one entry Object is missing, entryZ, then the array should be

{
 entryY,
 @"0.0",
 entryX
}

如果尝试了多种方法,但都没有真正起作用,我们将不胜感激.

If tried a number of ways but none really worked, any help is appreciated.

谢谢

更新: 更加明确: 假设parentArray按此顺序

UPDATE: MORE CLARITy: suppose parentArray is in this sequence

{parentA,parentB}

{parentA, parentB}

,并且条目数组位于

{entryX,entryY}

{entryX, entryY}

,我想重新排列entryArray和if

, I want to rearrange entryArray and if

entryY.parent == parentA和entryX.parent == parentB

entryY.parent == parentA, and entryX.parent == parentB

,然后根据其在parentArray中的位置,parentA是第一个,我希望entryY在entryX之前. result = {entryY, entryX}.

, then based on their postions in parentArray, parentA being the first, i'd want entryY to be before entryX. result = {entryY, entryX}.

推荐答案

实际上,您有一个使用选择器方法进行排序"(

In fact you have a "sort using selector method" (S.O. source):

- (NSComparisonResult)compareParents:(EntryClass*)otherObject {
    return [parentArray indexOfObject:self.parent] < [parentArray indexOfObject:otherEntry.parent];
}

NSArray *sortedArray;
sortedArray = [entryArray sortedArrayUsingSelector:@selector(compareParents:)];

这样做,在对entryArray进行排序时,将compareParents用作比较方法.这意味着如果e1.parent小于e2.parent,则e1将小于" e2.这正是您想要的.

Doing like this, when sorting entryArray, compareParents is used as a comparison method. That means that e1 will be "less than" e2 iff e1.parent is less than e2.parent. This is exactly what you want.

compareParents应该定义EntryClass的广告方法,并且parentArray应该可见.

compareParents should be defines ad method of EntryClass and parentArray should be visible to it.

原始答案:

我不确定我是否完全理解您要做什么.无论如何,我会使用字典解决这个问题,在字典中您将每个父项与其条目相关联.这样,对父数组进行排序后,就可以迭代并找出对应的条目,该条目也将(间接)进行排序.

I am not sure I understand fully what you are trying to do; anyway, I would approach the problem using a dictionary where you associate each parent to its entry. That way, once you have your parent array sorted, you can iterate and find out which is the corresponding entry, which will also be (indirectly) sorted.

这篇关于使用自定义类基于其他数组对NSArray进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆