简体   繁体   English

使用自定义类基于其他数组对NSArray进行排序

[英]Sorting NSArray based on other Array with custom classes

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;
}

This is the parent class, which fills up the parentArray 这是父类,它填充了parentArray

parentArray = { parentA, parentB, parentC }

This is another object class, it has ParentClass *parent. 这是另一个对象类,它具有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;

I want to sort this entryArray, according the sequence in parentArray. 我想根据parentArray中的顺序对这个entryArray进行排序。 So if ParentArray is: 因此,如果ParentArray为:

{
 ParentA,
 ParentB,
 ParentC
}

then I'd want the sorted entryArray to be 然后我想要排序的entryArray是

{
 entryY,
 entryZ,
 entryX
}

Also, if theres one entry Object is missing, entryZ, then the array should be 另外,如果缺少一个条目Object(条目Z),则该数组应为

{
 entryY,
 @"0.0",
 entryX
}

If tried a number of ways but none really worked, any help is appreciated. 如果尝试了多种方法,但都没有真正起作用,我们将不胜感激。

thanks 谢谢

UPDATE: MORE CLARITy: suppose parentArray is in this sequence 更新:更多声明:假设parentArray在此序列中

{parentA, parentB} {parentA,parentB}

, and entry array is in ,并且条目数组位于

{entryX, entryY} {entryX,entryY}

, I want to rearrange entryArray and if ,我想重新排列entryArray,如果

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

, then based on their postions in parentArray, parentA being the first, i'd want entryY to be before entryX. ,然后根据其在parentArray中的位置,parentA是第一个,我希望entryY在entryX之前。 result = {entryY, entryX}.

In fact you have a "sort using selector method" ( SO source ): 实际上,您有一个“使用选择器方法排序”( SO源 ):

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

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

Doing like this, when sorting entryArray , compareParents is used as a comparison method. 这样,在对entryArray进行排序entryArraycompareParents用作比较方法。 That means that e1 will be "less than" e2 iff e1.parent is less than e2.parent. 这意味着如果e1.parent小于e2.parent,则e1将“小于” e2。 This is exactly what you want. 这正是您想要的。

compareParents should be defines ad method of EntryClass and parentArray should be visible to it. compareParents应该定义EntryClass广告方法,并且parentArray应该对它可见。

ORIGINAL ANSWER: 原始答案:

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. 这样,对父数组进行排序后,就可以迭代并找出对应的条目,该条目也将(间接)进行排序。

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

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