Create student class name study number age score
Create five student objects, add arrays, sort by name ascending number descending ascending grade descending order
Main.m
1 #import<Foundation/Foundation.h>2 #import "Student.h"3 intMainintargcConst Char*argv[]) {4 @autoreleasepool {5 //arc4random ()%10 0---96 //7Student * S1 = [[Student alloc] Initwithname:@"Jack"Stuid:arc4random ()%Ten+1Age:arc4random ()%Ten+1Andscore:arc4random ()% A+ -];8 //60-1009 TenStudent * s2 = [[Student alloc] Initwithname:@"Tom"Stuid:arc4random ()%Ten+1Age:arc4random ()%Ten+1Andscore:arc4random ()% A+ -]; One AStudent * s3 = [[Student alloc] Initwithname:@"Lily"Stuid:arc4random ()%Ten+1Age:arc4random ()%Ten+1Andscore:arc4random ()% A+ -]; - -Student * S4 = [[Student alloc] Initwithname:@"Lucy"Stuid:arc4random ()%Ten+1Age:arc4random ()%Ten+1Andscore:arc4random ()% A+ -]; theStudent * S5 = [[Student alloc] Initwithname:@"Mark"Stuid:arc4random ()%Ten+1Age:arc4random ()%Ten+1Andscore:arc4random ()% A+ -]; - - //Nsarray * arr = @[s1,s2,s3,s4,s5]; -Nsmutablearray * arr =[Nsmutablearray array]; + [arr addobject:s1]; - [arr addobject:s2]; + [arr ADDOBJECT:S3]; A [arr ADDOBJECT:S4]; at [arr addobject:s5]; - //Grades Descending - for(inti =0; i< [arr Count]; i++) { - for(intj =0; J < [arr Count]-i-1; J + +) { -Student * S1 =Arr[j]; -Student * s2 = arr[j+1]; in if([S1 isstuscorelessthananother:s2]) { -[Arr exchangeobjectatindex:j withobjectatindex:j+1]; to } + } - } theNSLog (@"%@", arr); * $Nsarray * arr1 =[arr sortedarrayusingselector: @selector (isstuscorelessthananother:)];Panax NotoginsengNSLog (@"%@", arr1); - the //age left < right + ANsarray * arr2 = [arr sortedarrayusingcomparator:^nscomparisonresult (Student * S1, Student *S2) { the return[S1 Age] >[S2 age]; + }]; - $NSLog (@"%@", arr2); $ - -Nsarray * ARR3 = [arr sortedarrayusingcomparator:^nscomparisonresult (Student * obj1, Student *obj2) { the return[Obj1 Age] >[obj2 age]; - }];WuyiNSLog (@"%@", ARR3); the -Nsarray * ARR4 = [arr sortedarrayusingcomparator:^nscomparisonresult (Student * obj1, Student *obj2) { Wu return[Obj1 score] <[OBJ2 score]; - }]; AboutNSLog (@"%@", ARR4); $ - - } - return 0; A}
main.m
1 #import<Foundation/Foundation.h>2 3 @interfaceStudent:nsobject {4NSString *_name;5 int_stuid;6 int_age;7 int_score;8 }9- (ID) Initwithname: (NSString *) name Stuid: (int) Stuid Age: (int) Age Andscore: (int) score;Ten One- (int) score; A- (int) age; --(BOOL) Isstuscorelessthananother: (Student *) S2; - @end
Student.h
1 #import "Student.h"2 3 @implementationStudent4- (ID) Initwithname: (NSString *) name Stuid: (int) Stuid Age: (int) Age Andscore: (int) score {5 if(self =[Super Init]) {6_name =name;7_age =Age ;8_stuid =Stuid;9_score =score;Ten } One returnSelf ; A } - -- (int) score { the return_score; - } - --(NSString *) Description { + return[NSString stringWithFormat:@"name:%@ stuid:%d age:%d score:%d", _name,_stuid,_age,_score]; - } + A- (int) Age { at return_age; - } - --(BOOL) Isstuscorelessthananother: (Student *) s2 { - return[Self score] <[S2 score]; - } in - to @end
STUDENT.M
Student Sort Questions