Learn about NSMutableArray sorting instances in iPhone Development

Source: Internet
Author: User

UnderstandingIPhone DevelopmentAboutNSMutableArray sortingThe instance is the content to be introduced in this article, mainly to understandNSMutableArrayOfSortOperation, the content is not much, mainly based on the Code implementation content, look at the detailed code.

 
 
  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 shoshould returnNSOrderedAscending
  7. If the specified er is smaller than the argument, NSOrderedDescending if the specified er 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: Applicable to basic types that support the compare method)
  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: Suitable for the dict type. initWithKey is both the 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];

Summary: UnderstandingIPhone DevelopmentAboutNSMutableArray sortingThe instance content has been introduced. I hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.