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];xcode
[arrayaddObject:[NSNumbernumberWithInt:20]];app
[arrayaddObject:[NSNumbernumberWithInt:1]];less
[arrayaddObject:[NSNumbernumberWithInt:4]];oop
NSArray*sortedArray = [arraysortedArrayUsingSelector:@selector(compare:)];spa
for(inti =0; i < [sortedArraycount]; i++).net
{code
intx = [[sortedArrayobjectAtIndex:i]intValue];對象
NSLog(@"######%d\n", x);blog
}
(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
)