//// ArraryTest_OrderViewController.m// ArraryTest_Order//// Created by lv wei on 13-4-27.// Copyright (c) 2013年 lv wei. All rights reserved.//#import "ArraryTest_OrderViewController.h"@interfaceQStudent : NSObject@property(nonatomic,retain)NSString*studentID;-(NSComparisonResult)compare:(id)element;@end
@implementationQStudent@synthesizestudentID=_studentID;-(void)dealloc{[_studentIDrelease];[superdealloc];}//主要用来演示NSMutableArray的sortUsingSelector如何使用-(NSComparisonResult)compare:(QStudent*)element{intnum1=[_studentIDintValue];intnum2=[element.studentIDintValue];if(num1==num2){returnNSOrderedSame;}elseif(num1<num2){returnNSOrderedAscending;}returnNSOrderedDescending;}@end@interfaceArraryTest_OrderViewController()@property(nonatomic,retain)NSMutableArray*stringList;@end@implementationArraryTest_OrderViewController@synthesizestringList=_stringList;-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view, typically from a nib.NSMutableArray*stringList=[NSMutableArrayarrayWithObjects:@"90",@"23",@"1",@"1000",@"0",@"99",nil];self.stringList=stringList;[selfsortUsingComparator];[selfsortComparatorWithOptions];[selfsortUsingFuction];[selfsortUsingSelector];}-(void)sortUsingComparator{NSLog(@"sortUsingComparator");NSMutableArray*stringList=[_stringListmutableCopy];NSLog(@"原始的:%@",stringList);[stringListsortUsingComparator:^NSComparisonResult(idobj1,idobj2){if([obj1intValue]==[obj2intValue]){returnNSOrderedSame;}elseif([obj1intValue]>[obj2intValue]){returnNSOrderedDescending;}returnNSOrderedAscending;}];NSLog(@"排序后:%@",stringList);[stringListrelease];}-(void)sortComparatorWithOptions{NSLog(@"sortComparatorWithOptions");NSMutableArray*stringList=[_stringListmutableCopy];NSLog(@"原始的:%@",stringList);[stringListsortWithOptions:NSSortStableusingComparator:^NSComparisonResult(idobj1,idobj2){if([obj1intValue]==[obj2intValue]){returnNSOrderedSame;}elseif([obj1intValue]>[obj2intValue]){returnNSOrderedDescending;}returnNSOrderedAscending;}];NSLog(@"排序后:%@",stringList);[stringListrelease];}staticNSComparisonResultObjectCompare(idobj1,idobj2,void*context){if([obj1intValue]==[obj2intValue]){returnNSOrderedSame;}elseif([obj1intValue]>[obj2intValue]){returnNSOrderedDescending;}returnNSOrderedAscending;}-(void)sortUsingFuction{NSLog(@"sortUsingFuction");NSMutableArray*stringList=[_stringListmutableCopy];NSLog(@"原始的:%@",stringList);[stringListsortUsingFunction:ObjectComparecontext:nil];NSLog(@"排序后:%@",stringList);[stringListrelease];}-(void)sortUsingSelector{NSLog(@"sortUsingSelector");constintKArrayLen=6;intarray[KArrayLen]={90,23,1,1000,0,99};NSMutableArray*stringList=[[NSMutableArrayalloc]init];for(inti=0;i<KArrayLen;++i){QStudent*student=[[QStudentalloc]init];student.studentID=[NSStringstringWithFormat:@"%d",array[i]];[stringListaddObject:student];[studentrelease];}NSLog(@"原始的:");for(inti=0;i<KArrayLen;++i){NSLog(@"%@",[[stringListobjectAtIndex:i]studentID]);}[stringListsortUsingSelector:@selector(compare:)];NSLog(@"排序后:");for(inti=0;i<KArrayLen;++i){NSLog(@"%@",[[stringListobjectAtIndex:i]studentID]);}NSLog(@"反序后:");NSEnumerator*enumerator=[stringListreverseObjectEnumerator];[stringListrelease];stringList=[[NSMutableArrayalloc]initWithArray:[enumeratorallObjects]];for(inti=0;i<KArrayLen;++i){NSLog(@"%@",[[stringListobjectAtIndex:i]studentID]);}[stringListrelease];}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];// Dispose of any resources that can be recreated.}@end