Mithun
Mithun

Reputation: 479

Sort an array with respect to sorting of an another array iphone sdk

I have a situation here, I have two arrays,array1 and array2 and one tableview. I have loaded array1 to the table view. So here, I'm sorting array2, I've done that, but how can I sort array1 with respect to the change in array2 and to reload array1 in tableview. That is I need to sort array1 automatically when I sort array2. Any ideas? Please share your thoughts. Thanks :)

Upvotes: 1

Views: 276

Answers (2)

Can
Can

Reputation: 8571

The logical advice, would be that, if the objects are sorted together, then they're related somehow, and both pieces of data should conform a new object (A dictionary, an NSObject, whatever).

I mean, if array1 is [1, 5, 4, 3, 2] and array2 is ["hello", "world", "big", "little", "my"], then the mixed array would be:

[(1, "hello"), (5, "world"), (4, "big"), (3, "little"), (2, "my")].

Sorting this is trivial, and it makes sense if the data is correlated (no way to tell since you didn't specify this).

Objective-C example

Custom Object (CustomObject.h) that holds both name/number (using a number as a trivial example):

@interface CustomObject : NSObject

@property (nonatomic,retain) NSString *name;
@property (nonatomic,assign) NSInteger number;

+ (id)customObjectWithName:(NSString*)name andNumber:(NSInteger)number;

@end

CustomObject.m:

#import "CustomObject.h"

@implementation CustomObject

@synthesize name, number;

+ (id)customObjectWithName:(NSString*)name andNumber:(NSInteger)number
{
    CustomObject *customObject = [[CustomObject alloc] init];

    customObject.name = name;
    customObject.number = number;

    return customObject;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"(Number:%d, Name:%@)", self.number, self.name];
}

- (void)dealloc
{
    [name release];
    [super dealloc];
}

@end

Using these objects together with sorting:

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:
                            [CustomObject customObjectWithName:@"Hello" andNumber:1], 
                            [CustomObject customObjectWithName:@"gentlemen?" andNumber:5], 
                            [CustomObject customObjectWithName:@"you" andNumber:4],
                            [CustomObject customObjectWithName:@"are" andNumber:3], 
                            [CustomObject customObjectWithName:@"how" andNumber:2],
                                nil];

[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
    return [(CustomObject*)obj1 number] - [(CustomObject*)obj2 number];
}];

NSLog(@"Results: %@", array);

The output looks like:

2011-09-23 03:27:14.388 Test[5942:b303] Results: (
"(Number:1, Name:Hello)",
"(Number:2, Name:how)",
"(Number:3, Name:are)",
"(Number:4, Name:you)",
"(Number:5, Name:gentlemen?)"

Upvotes: 2

chown
chown

Reputation: 52728

Would need more info (source code or psudo) to determine how/when/where the sorting of array2 takes place to answer how array1 can be sorted; but once array1 does get sorted, you can reload the values into the table with:

[myTable reloadData];

And it will reload each row and cell, reading array1's values in their sorted order.

Upvotes: 0

Related Questions