title: Blocks in NSSortDescriptors - Objective C
body: <p>I have a <code>switch</code> statement which creates a relevant <code>NSSortDescriptor</code>. For some of the <code>NSSortDescriptors</code> I am using a <code>block</code> as a custom <code>comparator</code> (to compare <code>CMTimes</code>). The following code works fine but I would like to add some more <code>NSSortDescriptors</code> also comparing <code>CMTimes</code>. As the <code>block</code> is always the same is it possible to create a <code>variable</code> to hold the <code>block</code> so I don't need to keep copying and pasting messy code.  I guess it should be possible but I can't seem to get it to work.  I would greatly appreciate any help.  Thank you!</p>
  
  <pre><code>NSSortDescriptor *sortDescriptor; 
  
  switch (mode) {
      case 1:
          sortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"startTime" ascending: YES comparator:^(id first, id second){
                      CMTime time1 = [first CMTimeValue];
                      CMTime time2 = [second CMTimeValue];
                      if (CMTIME_COMPARE_INLINE(time1, &lt;, time2)) 
                          return NSOrderedAscending;
                      else if (CMTIME_COMPARE_INLINE(time1, &gt;, time2))
                          return NSOrderedDescending;
                      else
                          return NSOrderedSame;
          }];
          break;
      case 2:
          sortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"startTime" ascending: NO comparator:^(id first, id second){
                      CMTime time1 = [first CMTimeValue];
                      CMTime time2 = [second CMTimeValue];
                      if (CMTIME_COMPARE_INLINE(time1, &lt;, time2)) 
                          return NSOrderedAscending;
                      else if (CMTIME_COMPARE_INLINE(time1, &gt;, time2))
                          return NSOrderedDescending;
                      else
                          return NSOrderedSame;
          }];
          break;
      case 3:
          sortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"info" ascending: YES];
          break;
      case 4:       
          sortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"info" ascending: NO];
          break;
      default:
          break;
  }
  </code></pre>