0

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

3 Answers 3

3

Shouldn't you be sorting based on theCount key?

NSSortDescriptor * descriptor = [NSSortDescriptor sortDescriptorWithKey:@"theCount" ascending:NO];
[searchTermsArray sortUsingDescriptors:[NSArray arrayWithObject:descriptor]];
Sign up to request clarification or add additional context in comments.

Comments

2

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

4 Comments

Ok, and if you want to sort in descending order, then just swap NSOrderedAscending and NSOrderedDescending in the intSort function.
thanks for the answer, can u be slightly clearer regarding the params and context ?
It is basically the comparator function, which will define how your array will get sorted. We take parameters as id so that it supports all sort of objects. Since, your array contains Dictionary, we cast it to it and further tell the comparator function how the comparison has to be done. Also, please note that deepak's answer works too. It is short and nice.
Thanks pratikshabhisikar I appreciate the time and effort.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.