0

I create an NSCollectionView using cocoa binding.I find that when the NSMutableArray ,which is set as the NSCollectionView's content , finish to sort,the NSCollectionView has no change. Here is my sort code:

-(void)updateOrder
{

    [self willChangeValueForKey:@"bookmarksArray"];
    [_bookmarksArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

    /**
     Here is the sort code.
     **/

    }];
    [self didChangeValueForKey:@"bookmarksArray"];
}

I can catch the removed change and inserted change,but I can't catch the reorder change.Why?

gohamgx
  • 273
  • 2
  • 16

1 Answers1

1

This code will not actually change the order. sortedArrayUsingComparator: returns a whole new array. It doesn't sort an existing array in place. The sorting operation is effectively thrown away -- _bookmarksArray is never changed. You could, for instance, change it to:

-(void)updateOrder
{

    [self willChangeValueForKey:@"bookmarksArray"];
    _bookmarksArray = [_bookmarksArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

    /**
     Here is the sort code.
     **/

    }];
    [self didChangeValueForKey:@"bookmarksArray"];
}

(Assuming ARC here. Same idea but with appropriate retain/release calls if not ARC.)

ipmcc
  • 29,581
  • 5
  • 84
  • 147
  • The problem has been solved.I use the NSSortDescriptor.I try to your method.But your method is still the same result with my original problem. – gohamgx Jul 19 '13 at 14:42
  • According to your said "It doesn't sort an existing array in place",I think it's the answer. – gohamgx Jul 19 '13 at 14:46