0
votes

I have fetched some records from core data, and presented it in a table view. All of these records have 11 properties. Now in my tableview presentation, on header I have added a button for all of these properties. On clicking of the button, I want to sort the records on the basis of that property. I tried using sort descriptor, but it doesn't work. My code is like this:-

- (IBAction)sortButton:(id)sender {
// tags are from 100 to 111
UIButton *sortButton = (UIButton*)sender;
NSString *sortDescriptorKey = nil;
// create sort descriptor key according to the tag value
if (sortButton.tag == 100 )  sortDescriptorKey = [NSString stringWithString:@"reportID "];
....
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortDescriptorKey ascending:YES];
[self.recordArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
[self.tableView reloadData];
}

But, this code does not change any ordering at all. Am I missing anything, or is there is another way of doing it.

1

1 Answers

1
votes

The problem lies in this statement

[self.recordArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

This returns a sorted copy of recordArray, but you are not using it. You should assign it back to recordArray so that the tableView is correctly updated when it is reloaded. Change it to:

self.recordArray=[self.recordArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];