现在的位置: 首页 > 综合 > 正文

NSSortDescriptor的使用—排序

2018年01月08日 ⁄ 综合 ⁄ 共 3416字 ⁄ 字号 评论关闭

我们以数组的排序为例(也许NSSortDescriptor最常用的地方是NSFetchedResultsController中,但用法大致相同)。
    假设要对userArray数组中的对象进行排序,而数组中含有多个User对象(User继承于NSManagedObject),User中有一个属性叫做country。
[plain]
1. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"country" ascending:YES]; 
2. [userArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 
     这样,就可以根据每一个User的country来进行升序或降序的排列,sortUsingDescriptors的参数是一个数组,也就是说可以使用多个排序参数。
    再比如说,User有一个关系叫做image,image有个属性叫做timestamp,如果要根据User的image关系的timestamp排序,仅需把上面代码中的country改为,image.timestamp。即
[plain]
1. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"image.timestamp" ascending:YES]; 

2. [userArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 



在这里说明一点东西:
NSSortDescriptor是一个专门用来排序的。它可以设定关键字(字典中的key),根据对应key的value来进行一个排序。
其中,如果是对array数组进行排序,那么object为字典,并且key对应的value 一定得是string,如果对应的是对象obj,那么系统会报错。
一般用到的地方有NSArray(object 为NSDictionary),core data过滤筛选。

[csharp] view
plain
copy

  1. 根据字典中关键字,对字典进行排序    
  2. //First create the array of dictionaries    
  3. NSString *LAST = @"lastName";    
  4. NSString *FIRST = @"firstName";    
  5.      
  6. NSMutableArray *array = [NSMutableArray array];    
  7. NSArray *sortedArray;    
  8.      
  9. NSDictionary *dict;    
  10. dict = [NSDictionary dictionaryWithObjectsAndKeys:    
  11.                      @"Jo", FIRST, @"Smith", LAST, nil];    
  12. [array addObject:dict];    
  13.      
  14. dict = [NSDictionary dictionaryWithObjectsAndKeys:    
  15.                      @"Joe", FIRST, @"Smith", LAST, nil];    
  16. [array addObject:dict];    
  17.      
  18. dict = [NSDictionary dictionaryWithObjectsAndKeys:    
  19.                      @"Joe", FIRST, @"Smythe", LAST, nil];    
  20. [array addObject:dict];    
  21.      
  22. dict = [NSDictionary dictionaryWithObjectsAndKeys:    
  23.                      @"Joanne", FIRST, @"Smith", LAST, nil];    
  24. [array addObject:dict];    
  25.      
  26. dict = [NSDictionary dictionaryWithObjectsAndKeys:    
  27.                      @"Robert", FIRST, @"Jones", LAST, nil];    
  28. [array addObject:dict];    
  29.      
  30. //Next we sort the contents of the array by last name then first name    
  31.      
  32. // The results are likely to be shown to a user    
  33. // Note the use of the localizedCaseInsensitiveCompare: selector    
  34. NSSortDescriptor *lastDescriptor =    
  35.     [[[NSSortDescriptor alloc] initWithKey:LAST    
  36.                                ascending:YES    
  37.                                selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];    
  38. NSSortDescriptor *firstDescriptor =    
  39.     [[[NSSortDescriptor alloc] initWithKey:FIRST    
  40.                                ascending:YES    
  41.                                selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];    
  42.      
  43. NSArray *descriptors = [NSArray arrayWithObjects:lastDescriptor, firstDescriptor, nil];    
  44. sortedArray = [array sortedArrayUsingDescriptors:descriptors];  

[csharp] view
plain
copy

  1. <pre name="code" class="csharp"><pre name="code" class="csharp" style="margin-top: 4px; margin-right: 0px; margin-bottom: 4px; margin-left: 0px; background-color: rgb(240, 240, 240); "></pre><pre name="code" class="csharp" style="margin-top: 4px; margin-right: 0px; margin-bottom: 4px; margin-left: 0px; background-color: rgb(240, 240, 240); "></pre><pre name="code" class="csharp" style="margin-top: 4px; margin-right: 0px; margin-bottom: 4px; margin-left: 0px; background-color: rgb(240, 240, 240); "></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
  5. <pre></pre>  
  6. <pre></pre>  
  7. <pre></pre>  
  8. <pre></pre>  
  9. <pre></pre>  
  10.   
  11. </pre>  

抱歉!评论已关闭.