How to sort array of NSDates?

I wrote the following code to do so but I get a runtime exception saying


this class is not key value coding-compliant for the key date.


Here is my code...


NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:TRUE];
                [historyDateArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

What am I doing wrong?


History date array has all NSDates


Neerav

Accepted Reply

I passed nil as parameter for key and its working fine!

Replies

History date array has all NSDates

There's no instance property named `date` in `NSDate`, so your code fails.


Try something like this:

        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:TRUE];
        [historyDateArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

I tried that. The app crashed giving following error message...


Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[HistoryOperator length]: unrecognized selector sent to instance 0x283e55f50'

The name of my class is HistoryOperator.


Can you explain pass self?

I have checked my code with Command Line Tool project and it worked.

Of course my project does not contain a class named HistoryOperator. So, something you are not shown yet is causing the crash.


And `self` means itself. When applied to instances of `NSDate`, it means `NSDate` itself.

This code inside a file called HistoryOperator Do u mean pass the historyDateArray itself?

tried that as well. again the app crased.


the key needs a string.


its an array of nsdates which i want to sort in ascending order.

Do u mean pass the historyDateArray itself?

Sorry, I do not understand what you mean. My code does not contain `HistoryOperator`, and assuming `historyDateArray` is an `NSMutableArray` which has all NSDates. It has nothing to do with `HistoryOperator`.


If you say your code crashed, please show enough code to reproduce the issue.

I passed nil as parameter for key and its working fine!

Can you please show your code that causes crash you have shown?

I'm curious what's the difference. In my project both NULL and "self" works.

Sorry I am late to the fray:


I'm glad you got it to work. The reason why it was not working was because "key" is supposed to be a "key path" which is applied to the historyDateArray object to get values to sort. The value "date" is not a valid key path on historyDateArray. The value "nil" is an acceptable value for a key path.



See:

https://developer.apple.com/documentation/foundation/nssortdescriptor/1413572-initwithkey?language=objc

keyPath

The key path to use when performing a comparison.

For information about key paths, see Key-Value Coding Programming Guide.

......


Access properties by key path. When you have a hierarchy of key-value coding compliant objects, you can use key path based method calls to drill down, getting or setting a value deep within the hierarchy using a single call.