0

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 Answer 1

1

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

Comments

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.