sorting an array of NSdate

Discussion in 'iOS Programming' started by daproject85, Sep 12, 2012.

  1. daproject85, Sep 12, 2012
    Last edited by a moderator: Sep 12, 2012

    macrumors member

    Joined:
    Apr 13, 2011
    #1
    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?
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    You should know once you've look at the class reference for NSDate's compare: method, as well as NSArray's sortedArrayUsingSelector: method.
     
  3. thread starter macrumors member

    Joined:
    Apr 13, 2011
    #3
    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.
     
  4. macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #4
    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]];
      }
    ];
    
     
  5. thread starter macrumors member

    Joined:
    Apr 13, 2011
    #5
    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:)];
     
  6. macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #6
    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.
     

Share This Page