标签:des style blog http color 使用 os strong
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 *)dic 29 { 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 }else 35 { 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,
};
*/
#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选择器调用方法排序.
同时也适用于类对象.
2.高级排序,使用排序描述器:使用descriptor方法对Array中成员是类对象的类型进行按@propery 列进行排序.
上述代码就是我在管理系统中实现的,结果将在系统中呈现.
3.自定义重写方法进行排序.使用Block进行排序.
2013-11-29 08:42:56.723 OC实现学生成绩管理系统[483:303] 排序后:(
123,
1bc,
3ef,
4b6,
789
)
标签:des style blog http color 使用 os strong
原文地址:http://www.cnblogs.com/yunis/p/3884446.html