首页 > 代码库 > 数组排序
数组排序
1 #pragma mark 冒泡排序 2 - (void)userArrSort:(NSMutableArray *)userArr 3 { 4 int n = userArr.count; 5 int i,j; 6 NSDictionary *temp; 7 for(j=0;j<n-1;j++) 8 { 9 for(i=0;i<n-1-j;i++)10 {11 // NSDictionary*ssy = [];12 if(((NSString *)[userArr[i] objectForKey:@"vlevel"]).intValue<((NSString *)[userArr[i+1] objectForKey:@"vlevel"]).intValue)//数组元素大小按升序排列13 {14 temp=userArr[i];15 userArr[i]=userArr[i+1];16 userArr[i+1]=temp;17 }18 }19 }20 for (int x = 0; x<userArr.count; x++)21 {22 if (1) {23 // [userArr replaceObjectAtIndex:0 withObject:[userArr[i]];24 }25 }26 }27 #pragma mark 已排序数组插入一条数据28 - (void)insertObjectInUserArr:(NSMutableArray *)userArr withobject:(NSDictionary *)dic29 {30 int j;31 if(((NSString *)[dic objectForKey:@"vlevel"]).intValue<=((NSString *)[userArr[userArr.count - 1] objectForKey:@"vlevel"]).intValue)32 {33 [userArr insertObject:dic atIndex:userArr.count];34 }else35 {36 for(int i=0;i<userArr.count;i++)37 {38 if(((NSString *)[dic objectForKey:@"vlevel"]).intValue>((NSString *)[userArr[i] objectForKey:@"vlevel"]).intValue)39 {40 j = i;41 [userArr insertObject:dic atIndex:j];42 break;43 }44 }45 }46 47 48 }
(1)直接调用系统的方法排序int
NSMutableArray*array = [[NSMutableArrayalloc]init];
[arrayaddObject:[NSNumbernumberWithInt:20]];
[arrayaddObject:[NSNumbernumberWithInt:1]];
[arrayaddObject:[NSNumbernumberWithInt:4]];
NSArray*sortedArray = [arraysortedArrayUsingSelector:@selector(compare:)];
for(inti =0; i < [sortedArraycount]; i++)
{
intx = [[sortedArrayobjectAtIndex:i]intValue];
NSLog(@"######%d\n", x);
}
(2)用descriptor方法
#import<Cocoa/Cocoa.h>
@interfaceNode: NSObject {
intx;
inty;
intv;
}
@property intx;
@property inty;
@property intv;
- (id)init:(int)tx y:(int)ty v:(int)tv;
@end
@implementationNode
@synthesizex;
@synthesizey;
@synthesizev;
- (id)init:(int)tx y:(int)ty v:(int)tv {
x= tx;
y= ty;
v= tv;
return self;
}
@end
intmain(intargc,char*argv[])
{
NSMutableArray *myMutableArray = [[NSMutableArrayalloc]init];
Node *n[2];
n[0] = [[Nodealloc]init:2y:1v:1];
n[1] = [[Nodealloc]init:4y:2v:2];
[myMutableArrayaddObject:n[0]];
[myMutableArrayaddObject:n[1]];
NSSortDescriptor* sortByA = [NSSortDescriptorsortDescriptorWithKey:@"x"ascending:NO];
[myMutableArraysortUsingDescriptors:[NSArrayarrayWithObject:sortByA]];
for(Node*tinmyMutableArray) {
NSLog(@"x === %d", t.x);
NSLog(@"y === %d", t.y);
NSLog(@"v === %d", t.v);
}
}
(3)自定义重写方法
/*
Abstract:Constants returned by comparison functions, indicating whether a value is equal to, less than, or greater than another value.
Declaration:enumCFComparisonResult{
kCFCompareLessThan= -1,
kCFCompareEqualTo= 0,
kCFCompareGreaterThan= 1
};
*/
#import<Cocoa/Cocoa.h>
@implementation NSNumber (MySort)
- (NSComparisonResult) myCompare:(NSString *)other {
//这里可以作适当的修正后再比较
int result = ([selfintValue]>>1) - ([other intValue]>>1);
//这里可以控制排序的顺序和逆序
return result < 0 ?NSOrderedDescending : result >0 ?NSOrderedAscending :NSOrderedSame;
}
@end
int main(int argc, char *argv[])
{
NSMutableArray *array = [[NSMutableArrayalloc]init];
[arrayaddObject:[NSNumbernumberWithInt:20]];
[arrayaddObject:[NSNumbernumberWithInt:1]];
[arrayaddObject:[NSNumbernumberWithInt:4]];
NSArray *sortedArray = [arraysortedArrayUsingSelector:@selector(myCompare:)];
for(int i = 0; i < [sortedArraycount]; i++)
{
int x = [[sortedArrayobjectAtIndex:i]intValue];
NSLog(@"######%d\n", x);
}
}
c语言实现的学生成绩管理系统是面向过程的,而OC实现的学生成绩管理系统则是面向对象的.
对该系统的重难点讲述如下:
1.排序.系统中的四个关键字分别是 stuID,姓名,年龄,成绩.我们可以选择这四种方式并选择 升降序的方法进行排序.
系统中使用的是 NSmutableArray 存储数据.那么如何对对象是类成员的数组排序呢?
先看看NSMutableArray排序的几种方式.
1)使用@Seletor选择器调用方法排序.
- NSMutableArray *array = [[NSMutableArray alloc]init];
- [array addObject:[NSNumber numberWithInt:10]];
- [array addObject:[NSNumber numberWithInt:20]];
- [array addObject:[NSNumber numberWithInt:0]];
- [array addObject:[NSNumber numberWithInt:1]];
- [array addObject:[NSNumber numberWithInt:3]];
- NSLog(@"未排序的数组:%@",array);
- NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(compare:)];
- NSLog(@"yi排序的数组:%@",sortedArray);
- /*
- 2013-11-29 08:28:00.616 OC实现学生成绩管理系统[451:303] 未排序的数组:(
- 10,
- 20,
- 0,
- 1,
- 3
- )
- 2013-11-29 08:28:00.621 OC实现学生成绩管理系统[451:303] yi排序的数组:(
- 0,
- 1,
- 3,
- 10,
- 20
- )
- */
同时也适用于类对象.
- //直接实现静态方法,获取带有name和age的Person对象
- +(Person *)personWithAge:(int) age withName:(NSString *)name{
- Person *person = [[Person alloc] init];
- person.age = age;
- person.name = name;
- return person;
- }
- //自定义排序方法
- -(NSComparisonResult)comparePerson:(Person *)person{
- //默认按年龄排序
- NSComparisonResult result = [[NSNumber numberWithInt:person.age] compare:[NSNumber numberWithInt:self.age]];//注意:基本数据类型要进行数据转换
- //如果年龄一样,就按照名字排序
- if (result == NSOrderedSame) {
- result = [self.name compare:person.name];
- }
- return result;
- }
2.高级排序,使用排序描述器:使用descriptor方法对Array中成员是类对象的类型进行按@propery 列进行排序.
- -(void)SortStudentArray//
- {
- NSLog(@"按照学号排序请输入 (id)");
- NSLog(@"按姓名排序请输入(name)");
- NSLog(@"按照年龄排序请输入(age)");
- NSLog(@"按照成绩排序(score)请输入:");
- char charKey[10] ;
- scanf("%s",charKey);
- NSString *tempkey = [NSString stringWithUTF8String:charKey];
- NSString *key = [tempkey lowercaseString];
- BOOL ascending = NO;
- NSLog(@"是否开启降序模式( 默认不开启 )?(yes or no):");
- char ascendingStr[10] ;
- scanf("%s",ascendingStr);
- NSString *ascendingKey =[[NSString stringWithUTF8String:ascendingStr] lowercaseString];
- if ([ascendingKey isEqualToString:@"yes"])
- ascending = YES;
- else
- ascending = NO;
- if ([key isEqualToString:@"id"]) {
- NSSortDescriptor *sortByID = [NSSortDescriptor sortDescriptorWithKey:@"stuID" ascending:ascending];
- // 建立NSSortDescriptor 对象,按照的属性列,是否是asc升序?
- [studentArray sortUsingDescriptors:[NSArray arrayWithObject:sortByID]];
- //按照建立的descriptor进行排序.
- }
- else if([key isEqualToString:@"name"])
- {
- NSSortDescriptor *sortByName= [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:ascending];
- [studentArray sortUsingDescriptors:[NSArray arrayWithObject:sortByName]];
- }
- else if([ key isEqualToString:@"age"])
- {
- NSSortDescriptor *sortByAge = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:ascending];
- [studentArray sortUsingDescriptors:[NSArray arrayWithObject:sortByAge]];
- }
- else if ([key isEqualToString:@"score"])
- {
- NSSortDescriptor *sortByScore = [NSSortDescriptor sortDescriptorWithKey:@"score" ascending:ascending];
- [studentArray sortUsingDescriptors:[NSArray arrayWithObject:sortByScore]];
- }
- }
上述代码就是我在管理系统中实现的,结果将在系统中呈现.
3.自定义重写方法进行排序.使用Block进行排序.
- NSArray *array = [NSArray arrayWithObjects:@"1bc",@"4b6",@"123",@"789",@"3ef", nil nil];
- NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
- //这里的代码可以参照上面compare:默认的排序方法,也可以把自定义的方法写在这里,给对象排序
- NSComparisonResult result = [obj1 compare:obj2];
- return result;
- }];
- NSLog(@"排序后:%@",sortedArray);
2013-11-29 08:42:56.723 OC实现学生成绩管理系统[483:303] 排序后:(
123,
1bc,
3ef,
4b6,
789
)