2

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 = { parentA, parentB, parentC }

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;

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

{
 ParentA,
 ParentB,
 ParentC
}

then I'd want the sorted entryArray to be

{
 entryY,
 entryZ,
 entryX
}

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.

thanks

UPDATE: MORE CLARITy: suppose parentArray is in this sequence

{parentA, parentB}

, and entry array is in

{entryX, entryY}

, I want to rearrange entryArray and if

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

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

jasonIM
  • 195
  • 2
  • 13

1 Answers1

6

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:)];

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 should be defines ad method of EntryClass and parentArray should be visible to it.

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.

Community
  • 1
  • 1
sergio
  • 67,961
  • 11
  • 98
  • 119
  • Simply put I want to sort the entryArray based on the sequence of parent Objects in parentArray. Using dictionaries seems like the last resort, i was wondering if there could be a sortUsing selector kinda way.. – jasonIM Feb 11 '12 at 18:30
  • Hey thanks for replying, but, how can I use the parentArray ? Since this sorting isn't happening within the parent's class, its happening in a view controller, and the sequence of entryArray would depend on the sequence of parent objects in ParentARRAY... Really appreciate your help! – jasonIM Feb 11 '12 at 18:43
  • Well, I was suggesting an NSDictionary because I could not see a direct relationship between an entry and its parent. This is definitely required if you want to sort entries based on their parents. I don't know if you could reconsider your design and having each entry point to its own parent, not (or in addition) to the full parent array. – sergio Feb 11 '12 at 18:48
  • hmm, the direct relation ship is that entryX.parent == parentB, entryY.parent == parentA. I'd like the entries to be sorted based on the Array that includes only the parents = {parent A, parentB}, in which case the result should be {entryX, entryY}. Your right, nsdict does seem like the only way out, but I'll be doing this in cellForRowAtIndex. and I've considered the design part, but this is kinda the only way i got.. – jasonIM Feb 11 '12 at 18:52
  • if `entryX.parent` is exntryX's parent, and you want that entryX come before entryY whenever entryX's parent comes before entryY's parent, then the code I gave above will work. You need to sort the entries array in your view controller, not (or not only) the parents array. Can you view controller get a pointer to the entries array? – sergio Feb 11 '12 at 18:58
  • Im updating the question to add this, suppose parentArray is in this sequence {parntA, parentB}, and entry array is in {entryX, entryY}, I want to rearrange entryArray and if entryY.parent == parentA, and entryX.parent == parentB, then based on their postions in parentArray, parentA being the first, i'd want entryY to be before entryX. result = {entryY, entryX}. and the viewController has these two arrays, obviously parentArray is an NSArray (fixed) only need to sort entryArray.. – jasonIM Feb 11 '12 at 19:14
  • The whole problem is to sort this entries according to another array which is an attribute of each entryClass, i dunno if i'm explaining it well.. – jasonIM Feb 11 '12 at 19:29
  • Please, see my edit, and give a try to the method I suggest. If it does not work, please report why... I am pretty sure it works as you need. – sergio Feb 11 '12 at 19:35
  • Hey tried it, didn't quite work. " e2 iff e1.parent is less than e2.parent" Im probably saying something wrong, cuz Parent isn't a number its an Object class, e1's position is determined by 1], what its parent is? 2] what that parent's position is in the parentArray – jasonIM Feb 11 '12 at 19:50
  • sorry about the misunderstanding. see my new definition for `compareParents`; it uses `parentArray`, so it should be visible to it. – sergio Feb 11 '12 at 20:07
  • Hey, I'm trying to test that out, seems like the NSComparisonResult function you gave is for the Entry Object class, and since theres no preset array defined in the class, I can't pass it as parentArray, .. need to send that array from the view controller class. , I'm trying it out now. – jasonIM Feb 11 '12 at 21:06