1

Im working on displaying json data in table view and passing table view data to next view controller. Here i want to sort my json response based on name and then display in table view. here is my json data

"data": [
    {
        "_id": "597323707c691a6b27c63bcc",
        "name": "Adidas",
        "file": "http://104.236.67.117:5000/uploads/1500717927565.jpg",
        "logo": "http://104.236.67.117:5000/uploads/1500717910702.jpg",     
    },
    {
        "_id": "597323707c691a6b27c63bcc",
        "name": "Bibo",
        "file": "http://104.236.67.117:5000/uploads/1500717927565.jpg",
        "logo": "http://104.236.67.117:5000/uploads/1500717910702.jpg",     
    }, {
        "_id": "597323707c691a6b27c63bcc",
        "name": "Caprese",
        "file": "http://104.236.67.117:5000/uploads/1500717927565.jpg",
        "logo": "http://104.236.67.117:5000/uploads/1500717910702.jpg",     
    }, {
        "_id": "597323707c691a6b27c63bcc",
        "name": "DK",
        "file": "http://104.236.67.117:5000/uploads/1500717927565.jpg",
        "logo": "http://104.236.67.117:5000/uploads/1500717910702.jpg",     
    }

i am able to sort only name but not the whole object. Can any one solve this issue for me? It will be very helpful for me... thanks in advance.....

  • Use NSSortDescriptor. – phani Aug 17 '17 at 07:31
  • but i want to sort objects of array in the response – Charishma Vasamsetti Aug 17 '17 at 07:34
  • i am using this line to sort the json data sortedArray = [serverData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; here i am only able to sort single element. But question is how to sort total objects in an array – Charishma Vasamsetti Aug 17 '17 at 10:03
  • use "caseInsensitiveCompare:" instead of "localizedCaseInsensitiveC‌​ompare:" – phani Aug 17 '17 at 10:15
  • *********-[__NSDictionaryI rangeOfString:options:]: unrecognized selector sent to instance******** how can i solve this issue – Charishma Vasamsetti Aug 17 '17 at 10:37
  • Could you show me your sort descriptorCode – phani Aug 17 '17 at 10:46
  • NSIndexSet* indexes = [sortedArray indexesOfObjectsPassingTest:^BOOL(NSString *string, NSUInteger idx, BOOL *stop){ return [string rangeOfString:[brandsIndexTitles objectAtIndex:i] options:NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch | NSAnchoredSearch].location != NSNotFound; to display sorted array in table view in alphabetical order – Charishma Vasamsetti Aug 17 '17 at 10:57
  • In above question you asked about sorting an array. But here in this code you had taken NSIndexSet. Whats your exact need. – phani Aug 17 '17 at 11:02
  • i have already sorted my json data by the code which you have given now my question is when i tap on an alphabet like (when i tap on R) then it has to be moved to particular strings in the list ???? – Charishma Vasamsetti Aug 17 '17 at 11:05
  • like iPhone contacts screen? – phani Aug 17 '17 at 11:09
  • yeah absolutely... – Charishma Vasamsetti Aug 17 '17 at 11:12
  • Up to now I didn't implement this. I don't know how to do that. But your error says "Your trying to get the value which is not exist". may be indexPath:i is wrong. – phani Aug 17 '17 at 11:25
  • If you are unable to fix that issue. Send mail to me "phani697@gmail.com". – phani Aug 17 '17 at 11:58

3 Answers3

1
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    [arr sortUsingDescriptors:@[sortDescriptor]];

change it to swift code. Its working. I check this code with NSDictionaries.

phani
  • 105
  • 2
  • 15
0

in Objective C:(answer of user3306145)

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                    initWithKey: @"name" ascending: YES];

NSArray *sortedArray = [self.displayItems sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortDescriptor]];
-1

var descriptor: NSSortDescriptor = NSSortDescriptor(key: "name", ascending: true) var sortedResults: NSArray = results.sortedArrayUsingDescriptors([descriptor])

user3306145
  • 76
  • 2
  • 12