Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

daproject85

macrumors member
Original poster
Apr 13, 2011
37
0
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

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

now does this sort the array in ascending date or descending date? how would i know?
 
Last edited by a moderator:

daproject85

macrumors member
Original poster
Apr 13, 2011
37
0
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

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
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

Code:
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:


Code:
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:


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

daproject85

macrumors member
Original poster
Apr 13, 2011
37
0
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

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

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
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

Code:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.