Christian Krueger
Christian Krueger

Reputation: 139

Sorting a viewbased tableview with multiple sort descriptors doesn't work

I cannot get sorting working on a view based tableview for a CoreData App using multiple sort descriptors. I have 2 columns, my first column has "bank" and "accountNickName" as values , my second column has "bankAccount".
I want to sort by "bank" then "accountNickName" and then by "bankAccount". If I click on the 1st column.
I want to sort by "bankAccount", then "bank", then "accountNickName".
If I click on the second column.
Creating an array of sortDescriptors and bind this to my arraycontroller doesn't work:

sortForStockInfo = [ NSArray arrayWithObjects:
                            [NSSortDescriptor sortDescriptorWithKey:@"bank" ascending:YES selector:@selector(compare:)],
                            [NSSortDescriptor sortDescriptorWithKey:@"accountNickName" ascending:YES selector:@selector(compare:)],
                            [NSSortDescriptor sortDescriptorWithKey:@"account" ascending:YES selector:@selector(compare:)],
                            nil];

The tableview has "Sort Descriptors" bound to the Array Controller's "Sort Descriptors". I thought, this is all I had to do. But it doesn't work. What have I missed out ?

Strange enough: If I use the same approach, but I fill the columns attributes for Sortkey, Selector and Order, it is sorting only for one aspect (e.g. bank or account. accountNickName remains unsorted ) . Because I can only define one criteria per column.

Upvotes: 0

Views: 366

Answers (1)

Willeke
Willeke

Reputation: 15589

sortDescriptors is an array, the sortDescriptor of the clicked column is inserted at index 0. When the user clicks on column 0 and then on column1, the sort order is column1, column0. Implement delegate method - (void)tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn and set sortDescriptors of the arraycontroller. Example:

- (void)tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn {
    NSArray *sortDescriptors = self.arrayController.sortDescriptors;
    if (sortDescriptors && [sortDescriptors count] > 0) {
        NSSortDescriptor *firstSortDescriptor = sortDescriptors[0]; // sort descriptor of the clicked column
        BOOL ascending = firstSortDescriptor.ascending;
        if ([firstSortDescriptor.key isEqualToString:@"bank"]) {
            self.arrayController.sortDescriptors = @[
                [NSSortDescriptor sortDescriptorWithKey:@"bank" ascending:ascending selector:@selector(compare:)],
                [NSSortDescriptor sortDescriptorWithKey:@"accountNickName" ascending:ascending selector:@selector(compare:)],
                [NSSortDescriptor sortDescriptorWithKey:@"account" ascending:ascending selector:@selector(compare:)]];
        }
        else
            if ([firstSortDescriptor.key isEqualToString:@"account"]) {
                self.arrayController.sortDescriptors = @[
                    [NSSortDescriptor sortDescriptorWithKey:@"account" ascending:ascending selector:@selector(compare:)],
                    [NSSortDescriptor sortDescriptorWithKey:@"bank" ascending:ascending selector:@selector(compare:)],
                    [NSSortDescriptor sortDescriptorWithKey:@"accountNickName" ascending:ascending selector:@selector(compare:)]];
            }
    }
}

Upvotes: 3

Related Questions