[OC Learning note] Array traversal and sorting

Source: Internet
Author: User

1         //Array Traversal (example of a variable group)2         //For traversal3Nsmutablearray *AA =[Nsmutablearray array];4[AA addobjectsfromarray:@[@"SS",@"DDD",@"Sad"]];5          for(inti =0; i < Aa.count; i++) {6NSLog (@"%@", [AA objectatindex:i]);7         }8         //Fast Traversal9          for(IDObjinchAA) {TenNSLog (@"%@", obj); One         } A         //code block Traversal -         //obj is the object idx is subscript stop is a stop flag -[AA enumerateobjectsusingblock:^ (IDobj, Nsuinteger idx, BOOL *stop) { theNSLog (@"%@", obj); -NSLog (@"%@", [AA objectatindex:idx]); -         }]; -         //iterator Traversal +         //storing array elements in iterators -Nsenumerator *en =[AA Objectenumerator]; +         IDobj = nil;//create an object to accept the returned object A          while(obj =[en nextobject]) { atNSLog (@"%@", obj); -         } -         //Reverse iterator Traversal -Nsenumerator *en1 =[AA Reverseobjectenumerator]; -         IDObj1 =Nil; -          while(Obj1 =[En1 Nextobject]) { inNSLog (@"%@", obj1); -         } to          +          -         //Sort (Variable group example) the         //variable array sorting method no return value the order of the immutable group has a return value *         //Selector Sort $         //Compare method is limited to comparing elements is a character, if there is another comparison please write the comparison method return value (Nscomparisonresult)Panax Notoginseng [AA sortusingselector: @selector (compare:)]; -         //code block ordering the         //write the judging method according to the actual situation +[AA Sortusingcomparator:^nscomparisonresult (IDObj1,IDobj2) { A             return[Obj1 compare:obj2]; the         }]; +         //Compare Descriptor Ordering -         //ordering more than one condition is convenient $Person *P1 =[[Person alloc]init]; $P1.name =@"SS"; -P1.age =@" -"; -          thePerson *P2 =[[Person alloc]init]; -P2.name =@"DD";WuyiP2.age =@" -"; the          -Person *P3 =[[Person alloc]init]; WuP3.name =@"hh"; -P3.age =@" A"; About          $Person *P4 =[[Person alloc]init]; -P4.name =@"hh"; -P4.age =@" +"; -Nsmutablearray *sss =[Nsmutablearray Arraywithobjects:p1,p2,p3,p4,nil]; ANssortdescriptor *sort1 = [Nssortdescriptor sortdescriptorwithkey:@"name"Ascending:yes]; +Nssortdescriptor *sort2 = [Nssortdescriptor sortdescriptorwithkey:@" Age"Ascending:yes]; theNsarray *s =@[sort1,sort2]; - [SSS sortusingdescriptors:s]; $NSLog (@"%@", SSS);

Person.h

1 #import <Foundation/Foundation.h>23@interface  person:nsobject 4 @property (nonatomic,strong) NSString *name; 5 @property (nonatomic,strong) NSString * age; 6 @end

Person.m

1 #import "Person.h"2 3 @implementation Person4-(NSString *) Description5 {6     return[NSString stringWithFormat:@"%@ %@", _name,_age];7 }8 @end

Run result Analysis:

 -- .- on  -:Panax Notoginseng:55.984array[2030:140148] SS -- .- on  -:Panax Notoginseng:55.985array[2030:140148] DDD -- .- on  -:Panax Notoginseng:55.985array[2030:140148] Sad -- .- on  -:Panax Notoginseng:55.986array[2030:140148] SS -- .- on  -:Panax Notoginseng:55.986array[2030:140148] DDD -- .- on  -:Panax Notoginseng:55.986array[2030:140148] Sad -- .- on  -:Panax Notoginseng:55.986array[2030:140148] SS -- .- on  -:Panax Notoginseng:55.986array[2030:140148] SS -- .- on  -:Panax Notoginseng:55.986array[2030:140148] DDD -- .- on  -:Panax Notoginseng:55.987array[2030:140148] DDD -- .- on  -:Panax Notoginseng:55.987array[2030:140148] Sad -- .- on  -:Panax Notoginseng:55.987array[2030:140148] Sad -- .- on  -:Panax Notoginseng:55.992array[2030:140148] SS -- .- on  -:Panax Notoginseng:55.992array[2030:140148] DDD -- .- on  -:Panax Notoginseng:55.992array[2030:140148] Sad -- .- on  -:Panax Notoginseng:55.992array[2030:140148] Sad -- .- on  -:Panax Notoginseng:55.992array[2030:140148] DDD -- .- on  -:Panax Notoginseng:55.993array[2030:140148] SS -- .- on  -:Panax Notoginseng:55.993array[2030:140148] (    "DD",    "hh",    "hh",    "SS")

[OC Learning note] Array traversal and sorting

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.