chewy
chewy

Reputation: 8267

Objective-c Sort Key array within array

Assuming I have an NSMutableArray which is loaded from file:

searchTermsArray = [[NSMutableArray alloc] initWithContentsOfFile: yourArrayFileName];

inside this array items are key objects

for (int i=0; i<[searchTermsArray count]; i++) {
            NSLog(@"for array item %d: %@ - %@",i,[[searchTermsArray objectAtIndex:i] objectForKey:@"title"], [[searchTermsArray objectAtIndex:i] objectForKey:@"theCount"] );
        }

(which means that each array element (item) has 2 keys values:

searchTermsArray[0] = title (string) , theCount (also a string, but made out of integers)

Question: how should I sort "searchTermsArray" array from higher to lower based on "theCount" value?

(I am looking at the following code but it is not fitting the structure/syntax)

NSSortDescriptor *Sorter = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:NO];
        [searchTermsArray sortUsingDescriptors:[NSArray arrayWithObject:Sorter]];
        [Sorter release];

Upvotes: 0

Views: 1989

Answers (3)

user745098
user745098

Reputation:

I am not sure if there is a better way to do so. But this thing works.

NSInteger intSort(id param1, id param2, void *context) {

    NSDictionary *dict1 = (NSDictionary *)param1;
    NSDictionary *dict2 = (NSDictionary *)param2;

    NSInteger dict1KeyCount = [[dict1 objectForKey:@"count"] intValue];
    NSInteger dict2KeyCount = [[dict2 objectForKey:@"count"] intValue];

    if (dict1KeyCount < dict2KeyCount) {
        return NSOrderedAscending;
    }else if (dict1KeyCount > dict2KeyCount) {
        return NSOrderedDescending;
    }else {
        return NSOrderedSame;
    }
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad {
    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"two", @"title", @"2", @"count", nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"three", @"title", @"3", @"count", nil],
                                [NSDictionary dictionaryWithObjectsAndKeys:@"one", @"title", @"1", @"count", nil], nil];

    NSArray *sortedArray = [array sortedArrayUsingFunction:intSort context:NULL];

    for (NSDictionary *dict in sortedArray) {
        NSLog(@"%d", [[dict objectForKey:@"count"] intValue]);
    }

    [super viewDidLoad];
}

Upvotes: 2

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

Shouldn't you be sorting based on theCount key?

NSSortDescriptor * descriptor = [NSSortDescriptor sortDescriptorWithKey:@"theCount" ascending:NO];
[searchTermsArray sortUsingDescriptors:[NSArray arrayWithObject:descriptor]];

Upvotes: 3

Fervus
Fervus

Reputation: 720

The NSSortDescriptor is usually used to sort objects of a class. You pass the name of the property in that class to be compared with others. Since what you have in your array actually seems to be a NSDictionary, the NSSortDescriptor might not be the best way to approach this problem. Besides, your objects in the dictionary must have a type, so I would try to sort the array myself in one of the classic methods if I were you.

Upvotes: 0

Related Questions