-1

I'm implementing an app where i need to show a tableView. tableView needs to dispaly json data and have alphabetical search on the right side by clicking on any letter it needs to show data starting with that letter. I've implemented the vertical alphabet search and when user clicks on any letter it takes user to that particular section. But the problem is my json data is not getting sorted.

Here is my code for this. // Here exhibitorArray contains all the info to populate data in tableView.

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"serverData" ascending:YES];
NSArray *sortedArray = [serverData sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
NSLog(@"Sorted Array:%@",sortedArray);
  • You need to show at least ONE element of your array (or structure of `serverData`), and give us the key on which the array has to be sorted. – Larme Aug 04 '17 at 12:23
  • i could not understand your point can you please elaborate it – Charishma Vasamsetti Aug 04 '17 at 12:36
  • 1
    What's `serverData`? What does it looks like? – Larme Aug 04 '17 at 12:39
  • Can you print and give as your array values? – Abhishek Jain Aug 04 '17 at 12:39
  • serverData is an array from backend which has to be sorted in alphabetical order and displayed in table view – Charishma Vasamsetti Aug 04 '17 at 12:47
  • 2017-08-04 18:18:17.109 DankPass[7320:165833] Server Data is:( Lamborghini, Jergens, InStyle, HSBC, Heineken, Hartford, GMAC, Geico, Fanta, Famous, FedEx, Ferrari, Forbes, eBay, Enbridge, Dentyne, Daewoo, Disney, Darden, Compaq, Camel, BlackBerry, Adidas, "Fluid system", Crowdfunding, "Ice Stupa", "Artificial glacier", loboGo, bibo, "Big Bazar", "New Brand", "Main Brand Name" ) Here is the array – Charishma Vasamsetti Aug 04 '17 at 12:48
  • It's just an array of `NSString` then, so the `sortDescriptorWithKey:@"serverData"` won't work. I would have expected an error though on running. – Larme Aug 04 '17 at 13:23
  • i got the solution for my question  **********sortedArray = [serverData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; ******** the above code is working perfectly – Charishma Vasamsetti Aug 05 '17 at 06:06
  • now my question is when i tap on an alphabet like (when i tap on R letter) then it has to be moved to particular strings in the list ???? – Charishma Vasamsetti Aug 05 '17 at 06:54

1 Answers1

0
NSArray *unsortedStrings = @[@"Lamborghini", @"Lamborghini", @"InStyle",@"HSBC",@"Heineken"];
NSLog (@"unsorted = %@",unsortedStrings);

NSArray *sortedStrings =[unsortedStrings sortedArrayUsingSelector:@selector(compare:)];

NSLog (@"sorted = %@",sortedStrings);

unsorted = ( Lamborghini, Lamborghini, InStyle, HSBC, Heineken )

sorted = ( HSBC, Heineken, InStyle, Lamborghini, Lamborghini )

Chirag Desai
  • 827
  • 8
  • 13