Note that there are some explanatory texts on larger screens.

plurals
  1. PONSArray sortedArrayUsingDescriptors: New Copy or Retain?
    primarykey
    data
    text
    <p>According to <code>NSArray</code> class reference there are 4 type of methods to sort array:</p> <p>1- <code>sortedArrayUsingComparator</code>:</p> <p>2- <code>sortedArrayUsingSelector</code>:</p> <p>3- <code>sortedArrayUsingFunction:context</code>:</p> <p>4- <code>sortedArrayUsingDescriptors</code>:</p> <p>For first three methods it mentioned : The new array contains references to the receiving array’s elements, not copies of them. But for the forth method (descriptor) it mentioned: A copy of the receiving array sorted as specified by <code>sortDescriptors</code>.</p> <p>But following example shows like the other 3 methods, descriptor also retain original array and do not return a new copy of it:</p> <pre><code>NSString *last = @"lastName"; NSString *first = @"firstName"; NSMutableArray *array = [NSMutableArray array]; NSDictionary *dict; NSMutableString *FN1= [NSMutableString stringWithFormat:@"Joe"]; NSMutableString *LN1= [NSMutableString stringWithFormat:@"Smith"]; NSMutableString *FN2= [NSMutableString stringWithFormat:@"Robert"]; NSMutableString *LN2= [NSMutableString stringWithFormat:@"Jones"]; dict = [NSDictionary dictionaryWithObjectsAndKeys: FN1, first, LN1, last, nil]; [array addObject:dict]; dict = [NSDictionary dictionaryWithObjectsAndKeys: FN2, first, LN2, last, nil]; [array addObject:dict]; // array[0].first = "Joe" , array[0].last = "Smith" // array[1].first = "Robert" , array[1].last = "Jones" NSSortDescriptor *lastDescriptor =[[NSSortDescriptor alloc] initWithKey:last ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]; NSSortDescriptor *firstDescriptor =[[NSSortDescriptor alloc] initWithKey:first ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]; NSArray *descriptors = [NSArray arrayWithObjects:lastDescriptor, firstDescriptor, nil]; NSArray *sortedArray = [array sortedArrayUsingDescriptors:descriptors]; // array[1] == sortedArray[0] == ("Robert" , "Jones") // comparing array entries whether they are same or not: NSLog(@" %p , %p " , [array objectAtIndex:1] , [sortedArray objectAtIndex:0] ); // 0x10010c520 , 0x10010c520 </code></pre> <p>it shows objects in both arrays are same,</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload