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
2010-08-14 00:17:58
Thanks, looks good to me but where do I put it? I couldn't get it to work.
enbr
2010-08-14 15:38:19
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
2010-08-15 06:35:44
Can you paste another snippet explaining this please?
enbr
2010-08-15 14:23:21
I edited the code snippet above already. Try it again and see rows changed. Are you using NSArray or NSMutableArray as rows?
William
2010-08-15 22:29:04
that worked like a charm, thank you. by the way its a nsarray.
enbr
2010-08-17 17:12:33