Reputation: 1979
I have 3 MutableArray
's, but only want to use one of them, to sort all of them in the tableView.
jsonArray = [[NSMutableArray alloc] init]; //NAME
jsonStations = [[NSMutableArray alloc] init]; //URL
jsonSubtitle = [[NSMutableArray alloc] init]; //TABLEVIEW CELL SUBTITLE
I want to use the jsonArray
to sort all the json data in my tableView.
Heres how i get the json data:
NSURL *url = [NSURL URLWithString:@"http://www.xxxxx.com/xxx.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
if(connection)
{
jsonData = [[NSMutableData alloc] init];
}
...
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
NSArray *arrayOfItems = [allDataDictionary objectForKey:@"items"];
for (NSDictionary *diction in arrayOfItems) {
NSString *titles = [diction objectForKey:@"title"];
NSString *subtitles = [diction objectForKey:@"subtitle"];
NSString *station = [diction objectForKey:@"url"];
[jsonArray addObject:titles];
[jsonSubtitle addObject:subtitles];
[jsonStations addObject:station];
[self.tableview reloadData];
}
}
Sooo how do i sort my jsonArray by the titles
and show then sorted in the tableView ?
Upvotes: 3
Views: 1366
Reputation: 21808
This is how you can sort it:
NSArray *sortedArray;
sortedArray = [jsonArray sortedArrayUsingComparator:^NSComparisonResult(NSString *title1, NSString *title2)
{
if ([title1 compare:title2] > 0)
return NSOrderedAscending;
else
return NSOrderedDescending;
}];
[jsonArray setArray:sortedArray];
Upvotes: 1