views:

73

answers:

1

Hi, I am using TouchJSON to retrieve info for my app and put it in a dictionary. I would like to be able to sort it by values like difficulty and rating. How would I go about this? I have included my .m file. Thanks, enbr http://pastie.org/1091334

+1  A: 

You can probably use NSSortDescriptor to sort the array of dictionaries with specified keys. So, for example, the following can sort the array by the key "ratings" in each dictionary:

NSSortDescriptor *ratingsSortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"ratings" ascending:YES] autorelease];
rows = [rows sortedArrayUsingDescriptors:[NSArray arrayWithObject:ratingsSortDescriptor]];
William
Thanks, looks good to me but where do I put it? I couldn't get it to work.
enbr
My mistake: I was looking at the mutable array method (sortArrayUsingDescriptions:) and used the immutable array method. If rows is immutable (i.e. NSArray), the 2nd statement returns the sorted array, but rows remains unmodified, hence the assignment (which I missed). If rows is mutable (i.e. NSMutableArray), rows itself would be modified. I hope that makes sense....
William
Can you paste another snippet explaining this please?
enbr
I edited the code snippet above already. Try it again and see rows changed. Are you using NSArray or NSMutableArray as rows?
William
that worked like a charm, thank you. by the way its a nsarray.
enbr