Sort JSON NSDictionary for UITableView - iphone

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

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

Related

What will be my key in NSSortDescriptor

I want to sort my array in ascending and descending order. For that I have search on Google and thanks to StackOverflow I have got this answer,
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"inputtime" ascending:TRUE];
[myMutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
But I dont understand what will be the KEY which is in code inputtime. As you see all the data is provided is in NSMUtableArray and NSArray form than what will be key? I have tried to read apple document but still does not understand what is the key. Plz help me on this. Do i have to create dictionary?
You can use this code to sort array.
It helped me, and for this code thanx to #Anoop Vaidya
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"self" ascending:NO];
NSArray *descriptors=[NSArray arrayWithObject: descriptor];
NSArray *reverseOrder=[yourAry sortedArrayUsingDescriptors:descriptors];
Key is used when your array contain dictionary or Core data objects.
But when your array doesnt have any objects like this and containing simple NSString objects,you just need to use
"self" in place of key. It will surely work for you
Kane I think it will work for you-
NSArray *sortDescriptors = [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:#"time" ascending:NO]];
[request setSortDescriptors:sortDescriptors];
Here time is the key on basis of which i am fetching data in descending order. You can define a key on the basis of which you have to fetch. If there is no key then put self in place of key.
The Key will be on the basis of which you want to fetch data as in my example time is the key.
Hope this helps.

Can I sort an XML-fed array with NSStrings as though some of them were numbers?

I'm using NSXMLParser to fetch a feed, and am sorting the resulting array according to various properties. Result of parse is a bunch of NSStrings. Everything is working fine, except of course the one string with numbers greater than 10 in it. Array ends up getting ordered like so:
9,8,7,6,5,4,3,2,1,12,11,10,0
Obviously I want it like so:
12,11,10,9,8,7,6,5,4,3,2,1,0
I'm assuming this is because NSStrings get sorted "alphabetically", and that I'm going to have to convert to NSNumber or something like it? Is this the best way, or is there something simpler?
I currently have:
if ([elementname isEqualToString:#"rankicon"])
{
currentPlayer.rankicon = currentNodeContent;
}
and then:
NSSortDescriptor *rankDescriptor =
[[[NSSortDescriptor alloc]
initWithKey:#"rankicon"
ascending:NO]autorelease];
NSArray *sortDescriptors =
[NSArray arrayWithObjects:rankDescriptor, nil];
[players sortUsingDescriptors:sortDescriptors];
If you receive only numbers via xml then you can save them as NSNumbers instead.
NSString has also property intValue which returns that string as integer if it is possible.
This should work in your case (adding selector as parameter with value localizedStandardCompare) -
NSSortDescriptor * rankDescriptor = [[[NSSortDescriptor alloc] initWithKey:#"rankicon" ascending:NO selector:#selector(localizedStandardCompare:)] autorelease];

How to sort NSArray of objects based on one attribute

I am trying to sort an array of managed objects alphabetically. The attribue that they need to be sorted by is the name of the object (NSString) with is one of the managed attributes. Currently I am putting all of the names in an array of strings and then using sortedNameArray = [sortedNameArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)]; and then enumerating them back into an array with the objects. This falls apart when two names are the same so I really need to be able to sort by one attribute. How should I go about doing this?
Use NSSortDescriptor. Just search the documentation on it and there some very simple examples you can copy right over. Here is a simplified example:
NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:#"MyStringVariableName" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObject:valueDescriptor];
NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:descriptors];
And just like that you have a sorted array.
You can do this by using NSSortDescriptor,
eg.
`NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc]initWithKey:#"distance" ascending:YES];`
// Here I am sorting on behalf of distance. You should write your own key.
NSArray * descriptors = [NSArray arrayWithObject:valueDescriptor];
NSArray *sortedArray=[yourArray sortedArrayUsingDescriptors:descriptors];`

display data in alphabetical order iphone

How to display xml parsed Data in UITableView In alphabetical order.?
In order to display/arrange your data in alphabetical which in a array you have to use NSSortDescriptor
you have to make the object of this NSSortDescriptor class and give data here which you are fetching from XML
NSSortDescriptor *itemXml = [[NSSortDescriptor alloc] initWithKey:#"itemName" ascending:YES];
Now suppose you have an array sortDescriptors
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:itemXml,nil];
[yourArray sortUsingDescriptors:sortDescriptors];
Now give the yourArray to your UITableView delegate methods...... you will get the sorted data on table
NSSortDescriptor *sorter;
sorter = [[NSSortDescriptor alloc]initWithKey:#"Category" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sorter];
[categoryListArray sortUsingDescriptors:sortDescriptors];
[sorter release];
Try This
Actually there are lots of different ways to sort arrays.
Sort descriptors are only one example.
Others are sortedArrayUsingComparator, sortedArrayUsingFunction:context, sortedArrayUsingSelector, and new to Mac OS 10.6 and iOS 4.0, sortedArrayWithOptions:usingComparator.
Those are NSArray methods that return a sorted array.
NSMutableArray also has variations on those methods that sort the mutable array "in place".

NSSortdescriptor, finding the path to the key I wish to use for sorting

I am using an NSSortdescriptor to sort a collection of NSArrays, I then came across a case where the particular NSArray to be sorted contains an NSDictionary who contains an NSDictionary.
I would like to sort from the string paired with a key in the last dictionary.
This is how I would reference the string:
NSDictionary *productDict = [MyArray objectAtIndex:index];
NSString *dealerName = [[productDict objectForKey:#"dealer"] objectForKey:#"name"];
How would I use the dealerName in my NSSortdescriptor to sort the array?
NSSortDescriptor * sortDesc = [[NSSortDescriptor alloc] initWithKey:/* ? */ ascending:YES];
sortedDealerArray = [value sortedArrayUsingDescriptors:sortDesc];
[sortDesc release];
Hope someone could help me a bit with how I go about sorting according to keys inside objects inside other objects:)
Thank you.
actually warrenM is right. You can just use keypaths. #"dealer.name" should work for above.