user2452250
user2452250

Reputation: 797

Sort NSTableView bound to NSArrayController based on a undisplayed value

I have a NSArrayController with Objects (Mutable dictionaries) taht contain 2 values for each entry. The values are "keyword" and "order".

The NSArrayController is bound to a NSTableView that has only one column and is displaying the "keyword" value.

I would need to sort the table based on "order".

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"order" ascending:YES];
[keywordscontroller.arrangedObjects sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];

But the order in the NSTableView is not updating. what am I missing?

---EDIT ---

sample of how it is sorting:

enter image description here

Upvotes: 0

Views: 128

Answers (1)

Andrew Monshizadeh
Andrew Monshizadeh

Reputation: 1784

You are not storing the result of your call to - (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors and so, you will never have access to it.

However, since you already are using an NSArrayController, why not just use the - (void)setSortDescriptors:(NSArray *)sortDescriptors method of your NSArrayController and then reference the contents of the array using - (id)arrangedObjects?


custom sort descriptor for string with numeric comparison

[keywordscontroller setSortDescriptor:[NSSortDescriptor sortDescriptorWithKey:@"order" ascending:YES comparator:^(id obj1, id obj2) {
    return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
}];

Upvotes: 1

Related Questions