3

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 ?

3
  • 1
    XCode is the development environment. It's your editor, and it compiles and links your app. It does not do anything with your JSON. For that, you use Objective-C and the iOS library. I've changed the title and the tags accordingly. Commented Dec 26, 2012 at 16:04
  • [self.tableview reloadData]; is this works properly ? if so all you need to do is sort jsonArray before calling [self.tableview reloadData]; Commented Dec 26, 2012 at 16:09
  • As far as i can see.. yes.. but i haven't debug'ed it tho Commented Dec 26, 2012 at 16:14

1 Answer 1

1

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];
Sign up to request clarification or add additional context in comments.

4 Comments

Where shall i insert that ?
but don't just INSERT this piece of code. that's just an example of how to sort
Ah okay.. is it possible you could make the sort function to fit my code ? in new to json, and not quite into Obj-c
i've added a line. if your TableView takes data from jsonArray that should be enough

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.