了解iPhone开发中关于NSMutableArray排序实例

移动开发 iOS
了解iPhone开发中关于NSMutableArray排序实例是本文要介绍的内容,主要是来了解NSMutableArray的排序操作,具体实现来看本文详解。

了解iPhone开发中关于NSMutableArray排序实例是本文要介绍的内容,主要是来了解NSMutableArray排序操作,内容不多,主要是基于代码实现内容,来看详细代码。

  1. - (NSArray *)sortedArrayUsingSelector:(SEL)comparator  
  2.  
  3. Parameters  
  4. comparator  
  5.  
  6. A selector that identifies the method to use to compare two elements at a time. The method should returnNSOrderedAscending 
  7. if the receiver is smaller than the argument, NSOrderedDescending if the receiver is larger than the argument,
  8.  and NSOrderedSame if they are equal  
  9. NSArray *sortedArray =  
  10.  
  11. [anArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];  
  12. @property (nonatomic, readwrite, retain) NSMutableArray *parameters;  
  13. [self.parameters sortUsingSelector:@selector(compare:)];  
  14. #pragma mark -  
  15.  
  16. - (NSComparisonResult)compare:(id)inObject {  
  17.      NSComparisonResult result = [self.name compare:[(MPURLRequestParameter *)inObject name]];  
  18.      if (result == NSOrderedSame) {  
  19.  result = [self.value compare:[(MPURLRequestParameter *)inObject value]];  
  20.      }  
  21.       return result;  
  22. }  
  23.  
  24. sortedArrayUsingFunction:适合基本类型(支持compare方法)  
  25.  
  26. #pragma mark SORT METHOTDS  
  27. NSInteger sortObjectsByLatestTime(id obj1, id obj2, void *context)  
  28. {  
  29.  NSDate* d1 = [(MessageGroup*)obj1 latestTime];  
  30.  NSDate* d2 = [(MessageGroup*)obj2 latestTime];   
  31.  //sort by desc  
  32.  return [d2 compare:d1];  
  33. }  
  34. NSInteger dateSort(id obj1, id obj2, void *context)  
  35.  
  36. {  
  37.  NSDate* d1 = ((Inbox*)obj1).datetime;  
  38.  NSDate* d2 = ((Inbox*)obj2).datetime;  
  39.  return [d1 compare:d2];  
  40. }  
  41.  
  42. -(NSArray*)sortedMessages  
  43. {  
  44.  return [[groupMessages allValues] sortedArrayUsingFunction:sortObjectsByLatestTime context:NULL];  
  45. }  
  46.  
  47. sortUsingDescriptors:适合元素是dict类型,initWithKey既是dict key.  
  48.  
  49. NSMutableArray *regions = [NSMutableArray array];  
  50. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];  
  51. NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];  
  52. [regions sortUsingDescriptors:sortDescriptors];  
  53. [sortDescriptor release]; 

小结:了解iPhone开发中关于NSMutableArray排序实例的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 网易博客
相关推荐

2011-08-04 17:19:49

iPhone开发 Xcode 文档

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-07-29 13:27:48

iPhone 开发 Nib

2011-08-22 14:21:24

iPhone开发UIView Anim

2011-08-15 13:44:07

iPhone开发UITableView

2011-07-25 18:02:51

iPhone LibFetion 移植

2011-08-08 16:56:44

iPhone 字符处理 视图

2011-08-08 14:07:49

iPhone开发 字体

2011-08-16 18:56:11

iPhone开发Three20

2011-08-15 09:58:25

iPhoneXib文件UITableView

2011-08-02 13:35:41

iOS开发 Get Post

2011-08-08 10:23:41

iPhone 流播放 文件

2011-08-19 10:35:19

iPhone应用Three20

2011-07-06 17:40:43

iPhone SDK

2011-07-19 15:33:57

iPhone

2011-08-18 10:39:46

iPhone开发界面

2011-07-21 17:00:59

iPhone UIWebView Status Cod

2011-07-28 10:11:54

iPhone开发 备忘

2011-08-08 15:56:18

iPhone 震动 NSUserDefa

2011-07-27 11:19:33

iPhone UITableVie
点赞
收藏

51CTO技术栈公众号