5

I have 2 NSMutableArrays. First array contains custom object intances with property NSString *itemID, second array contains only from NSString objects with same values of itemID, but in another order. I need to sort first array by itemID property of each object, and it should be sorted like second array.

How I can do this?

4
  • array is short, based on one key ? Commented Apr 11, 2013 at 8:53
  • stackoverflow.com/questions/805547/… Commented Apr 11, 2013 at 9:00
  • I've seen this solution, and it's not suitable for my question Commented Apr 11, 2013 at 9:18
  • but good question ---- >+1 Commented Apr 11, 2013 at 10:37

3 Answers 3

3
guideArray = < YOUR SECOND ARRAY WITH STRING OBJECT >;    
unsortedArray = < YOUR FIRST ARRAY WITH CUSTOM OBJECT >;

[unsortedArray sortUsingComparator:^(id o1, id o2) {
    Items *item1 = o1;
    Items *item2 = o2;
    NSInteger idx1 = [guideArray indexOfObject:item1.ItemID];
    NSInteger idx2 = [guideArray indexOfObject:item2.ItemID];
    return idx1 - idx2;
}];
NSLog(@"%@",unsortedArray);
Sign up to request clarification or add additional context in comments.

Comments

1

Store the custom objects in an dictionary with itemID as key, use this dictionary as lookup to sort the objects:

    NSArray *objects; // your objects
    NSMutableArray *hintArray; // your sorted IDs
    NSMutableDictionary *lookupDict = [[NSMutableDictionary alloc] initWithCapacity:[objects count]];
    NSMutableArray *sortedObjects = [[NSMutableArray alloc] initWithCapacity:[hintArray count]];

    for (id object in objects) {
        [lookupDict setValue:object forKey:[object itemID]];
    }

    for (id hint in hintArray) {
        [sortedObjects addObject:[lookupDict valueForKey:hint]];
    }

EDIT: Solution with inplace sort of objects:

    NSMutableArray *objects;
    NSMutableArray *hintArray;
    NSMutableDictionary *lookupDict = [[NSMutableDictionary alloc] initWithCapacity:[hintArray count]];

    int i = 0;
    for (NSString *itemID in hintArray) {
        [lookupDict setValue:[NSNumber numberWithInt:i] forKey:itemID];
        i++;
    }

    [objects sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [[lookupDict valueForKey:[obj1 itemID]] compare:[lookupDict valueForKey:[obj2 itemID]]];
    }];

2 Comments

NSDictionary adds not to end of the list. So it wouldn't help
I dont get your comment. I simple build lookup dictionaries to avoid searching for each item in the sorted id-list, which will become very expensive when you got a large amount of items.
1

You can compare your two objects using following syntax :-

[items sortUsingComparator:^NSComparisonResult(Attribute *obj1, Attribute *obj2) 
{
    return [[NSNumber numberWithInt:[stringOrder indexOfObject:obj1.itemID]] compare:[NSNumber numberWithInt:[stringOrder indexOfObject:obj2.itemID]]]
}];

or else you can use following snippet :

NSArray* sortedKeys = [dict keysSortedByValueUsingComparator:^(id obj1, id obj2)
 {
    return [obj1 compareTo:obj2];
 }

Enjoy Programming !

1 Comment

Thanks, this is also working answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.