PDA

View Full Version : sorting an array of NSdate




daproject85
Sep 12, 2012, 06:29 PM
Hi Forum,

I want to sort an array of NSDate. I have been doing some googling and aside from using descriptors it can be done like this

sortedArray = [unsortedArray sortedArrayUsingSelector:@selector(compare:)];

now does this sort the array in ascending date or descending date? how would i know?



dejo
Sep 12, 2012, 07:11 PM
now does this sort the array in ascending date or descending date? how would i know?

You should know once you've look at the class reference for NSDate's compare: method, as well as NSArray's sortedArrayUsingSelector: method.

daproject85
Sep 12, 2012, 07:48 PM
You should know once you've look at the class reference for NSDate's compare: method, as well as NSArray's sortedArrayUsingSelector: method.

Hi dejo,

thanks for you reply. I had looked at the Apple docs and I did not understand it. I am new to Objective-C and iOS programming. If I had understood it I would not have bothered posting on the forum for help.

Duncan C
Sep 12, 2012, 09:26 PM
Hi Forum,

I want to sort an array of NSDate. I have been doing some googling and aside from using descriptors it can be done like this

sortedArray = [unsortedArray sortedArrayUsingSelector:@selector(compare:)];

now does this sort the array in ascending date or descending date? how would i know?

I always have to puzzle this sort of thing out, or just run a quick test. I'm too tired for either right now. I think it would sort into smallest-to-largest order, but I'm not positive.

There are about half-a-dozen different ways to sort arrays in Cocoa. sortedArrayUsingSelector is one.

Another useful method is sortedArrayUsingComparator: That method is very powerful, once you get your head around using blocks. For something simple like sorting an array of dates, it's overkill, since sortedArrayUsingSelector works perfectly. If you're sorting something like an array of dictionaries that contain dates, however, sortedArrayUsingComparator makes it pretty easy.

The idea is that you write a block of code that returns an NSComparisonResult value just like the method used in sortedArrayUsingSelector, but the comparator block is a block of code you write rather than a method that has to be defined in the objects you're sorting.

To do the same thing as sortedArrayUsingSelector using a comparator for your array of NSDates, you'd use code like this:


sortedArray = [datesArray sortedArrayUsingComparator:
^(id obj1, id obj2)
{
return [(NSDate*) obj1 compare: (NSDate*)obj2];
}
];


To reverse the order, you'd just switch the order of the objects in the call to compare:


sortedArray = [datesArray sortedArrayUsingComparator:
^(id obj1, id obj2)
{
return [(NSDate*) obj2 compare: (NSDate*)obj1];
}
];

daproject85
Sep 12, 2012, 11:02 PM
i guess my main question is after reading the apple docs I see it saying



compare:
Returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date.

- (NSComparisonResult)compare:(NSDate *)anotherDate
Parameters
anotherDate
The date with which to compare the receiver.
This value must not be nil. If the value is nil, the behavior is undefined and may change in future versions of OS X.
Return Value
If:

The receiver and anotherDate are exactly equal to each other, NSOrderedSame
The receiver is later in time than anotherDate, NSOrderedDescending
The receiver is earlier in time than anotherDate, NSOrderedAscending.


so that means this method returns an NSCOparisonResult (NSOrderedSame).... so then arn't we suppose to give some kind of parameters to this code? I dont see how we can really get anything back from it

sortedArray = [unsortedArray sortedArrayUsingSelector:@selector(compare:)];

Duncan C
Sep 15, 2012, 08:58 AM
i guess my main question is after reading the apple docs I see it saying




so that means this method returns an NSCOparisonResult (NSOrderedSame).... so then arn't we suppose to give some kind of parameters to this code? I dont see how we can really get anything back from it

sortedArray = [unsortedArray sortedArrayUsingSelector:@selector(compare:)];

Sort methods take some sort of comparison method/function/block as input. The sort method then runs through all the elements in the array, using the code you provide to compare pairs of objects and swap them if they are out of order.

The compare method you're using is a method of NSDate. It is a message you send to one date object, passing another date object as input. It tells you if the receiver comes before, matches, or comes after, the other date.