OCdemo - 05 OC中快速对数组进行排序

//
//  main.m
//  OCdemo-05-shuzupaixu
//
//  Created by lanou3g on 15/10/13.
//  Copyright (c) 2015年 Object. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Student.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
      
        /*
         
        NSMutableArray *marray = [NSMutableArray arrayWithObjects:@"alu",@"shengsheng",@"tingting",@"jinjin",@"xiaoqiangzi",@"wenwen",nil];
        
        NSLog(@"%@",marray);
        // 冒泡排序
        for (int i = 0; i < marray.count - 1; i++) {
            for (int j = 0; j < marray.count - i - 1; j++) {
                if ([marray[j] compare:marray[j + 1]] == NSOrderedDescending) {
                    //升序排列
                    [marray exchangeObjectAtIndex:j withObjectAtIndex:(j + 1)];
                }
            }
        }
        NSLog(@"%@",marray);
        
        NSMutableArray *mArray1 = [NSMutableArray arrayWithObjects:@10,@8,@3,@17,@9, nil];
        //冒泡排序
        for (int i = 0; i < mArray1.count; i++) {
            for (int j = 0; j < mArray1.count - i - 1; j++) {
                //数值对象比较
                id obj1 = mArray1[j];
                id obj2 = mArray1[j + 1];
                // 把数值对象转成整型
                int value1 = [obj1 intValue];
                int value2 = [obj2 intValue];
                
                if (value1 > value2) {
                    [mArray1 exchangeObjectAtIndex:j withObjectAtIndex:(j + 1)];
                }
            }
        }
        NSLog(@"%@",mArray1);
        //--------------------------------------------------------------------------
        NSArray *array1 = @[@"zhengzheng",@"zhusong",@"chaochao",@"junjun",@"shishi"];
        array1 = [array1 sortedArrayUsingSelector:@selector(compare:)];  //默认升序
        NSLog(@"%@",array1);
        
        NSMutableArray *mArray2 = [NSMutableArray arrayWithObjects:@"xiaocui",@"yizhang",@"junjun",@"xiaojie",@"haifeng",@"shengsheng" ,nil];
        [mArray2 sortUsingSelector:@selector(compare:)];
        NSLog(@"%@",mArray2);
        //--------------------------------------------------------------------------
         
         */
        
        
        Student *student1 = [Student studentWithName:@"da大黄" age:1];
        Student *student2 = [Student studentWithName:@"jian见见" age:23];
        Student *student3 = [Student studentWithName:@"xiao小强子" age:25];
        Student *student4 = [Student studentWithName:@"huang皇上" age:24];
        Student *student5 = [Student studentWithName:@"cun村长" age:25];
        
        NSMutableArray *studentArray = [NSMutableArray arrayWithObjects:student1,student2,student3,student4,student5, nil];
        
        //按照姓名排序
        [studentArray sortUsingSelector:@selector(compareStudentByName:)];
        for (Student *stu in studentArray) {
            NSLog(@"name = %@, age = %ld",[stu getName],[stu getAge]);
        }
        
        //按照年龄排序
        [studentArray sortUsingSelector:@selector(compareStudentByAge:)];
        for (Student *stu in studentArray) {
            NSLog(@"name = %@, age = %ld",[stu getName],[stu getAge]);
        }

        
    }
    return 0;
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------

//
//  Student.h
//  OCdemo-05-shuzupaixu
//
//  Created by lanou3g on 15/10/13.
//  Copyright (c) 2015年 Object. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Student : NSObject
{
    NSString *_name;  //姓名
    NSInteger _age;   //年龄
}
-(void)setName:(NSString *)name;
-(void)setAge:(NSInteger)age;

-(NSString *)getName;
-(NSInteger)getAge;


//自定义初始化方法
-(instancetype)initWithName:(NSString *)name
                        age:(NSInteger)age;
//便利构造器
+(instancetype)studentWithName:(NSString *)name
                           age:(NSInteger)age;

//按照姓名排序
-(NSComparisonResult)compareStudentByName:(Student *)otherStudent;

//按照年龄排序
-(NSComparisonResult)compareStudentByAge:(Student *)otherStudent;



@end
-----------------------------------------------------------------------------------------------------------------------------------------------------------
//
//  Student.m
//  OCdemo-05-shuzupaixu
//
//  Created by lanou3g on 15/10/13.
//  Copyright (c) 2015年 Object. All rights reserved.
//

#import "Student.h"

@implementation Student

-(void)setName:(NSString *)name{
    _name = name;
}
-(void)setAge:(NSInteger)age{
    _age = age;
}

-(NSString *)getName{
    return _name;
}
-(NSInteger)getAge{
    return _age;
}

//自定义初始化方法
-(instancetype)initWithName:(NSString *)name
                        age:(NSInteger)age{
    self = [super init];
    if (self) {
        _name = name;
        _age = age;
    }
    return self;
}
//便利构造器
+(instancetype)studentWithName:(NSString *)name
                           age:(NSInteger)age{
    Student *student = [[Student alloc] initWithName:name age:age];
    return student;
}
//按照姓名排序
-(NSComparisonResult)compareStudentByName:(Student *)otherStudent{
    if ([[self getName]compare:[otherStudent getName]] == NSOrderedAscending) {
        return NSOrderedAscending; //升序排列
    }else if ([[self getName] compare:[otherStudent getName]] == NSOrderedDescending){
        return NSOrderedDescending; //降序
} else {
    return NSOrderedSame;  // 相等
  }
    
    return [[self getName] compare:[otherStudent getName]];
}

//按照年龄排序
-(NSComparisonResult)compareStudentByAge:(Student *)otherStudent{
    
    if ([self getAge] > [otherStudent getAge]) {
        return NSOrderedDescending;  //降序
    } else if( [self getAge] < [otherStudent getAge] ){
        return NSOrderedAscending;   //升序
    }else{
        return NSOrderedSame;
    }
    
    
}




@end


1、使用Category,为NSArray添加两个公有方法;(10分) 1)计算NSArray所有对象值的和(针对NSString及NSNumber对象) 2)实现查找功能(不使用系统自带查找方法,- (BOOL)containsObject:(id)anObject;使用类目实现其原理) 2、新建Teacher、Student类,并完成下列操作:(30分) (1) Teacher类持有5个学生(students),每个学生拥有姓名和4门课程考试分数集合;(5分) (2)实现Student的方法来计算当前学生的平均分;(5分) (3)声明一个Student的委托协议和必选一个协议方法,该协议方法可以在 学生计算完成平均分后告诉委托计算结果;(5分) (4)让Teacher遵守(3)协议并与每个Student间建立委托的设计模式, 在Student平均分计算完成之后Teacher对每个学生平均分进行记录, 并输出。(10分) (5)给Teacher添加一个方法,将students数组找出平均分最高的学生。(5分) 3、模拟一个商品销售系统,完成下列问题:(60分) 1)现有三种打折商品:书、茶杯、钢笔,每种商品都包含共有的三种属性: 编号、售价、折扣率,最终售价为当前售价与折扣率的乘积; 2)可将三种商品根据商品名称或则编号的形式,添加入购物车,客户不需要时,可移除购物车指定的商品,客户可以随时查看购物车列表,显示购物车所有商品的详细信息; 3)最后客户离开时,可以结账离开; 注意: 1)可封装一个类,专用于用户输入信息; 2)代码冗余的处理; 3)用户体验; 4)代码规范,命名、细节点处理;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值