• Sort方法的扩展


    OC中类方法中仅仅为我们提供了一些降序方法,如今我们自定义方法,实现升序.

    1.要求:定义一个Person类,实例变量包含name,age,height,定义几个对象,把这些对象保存在数组中,自定义方法,实现数组按name,age,gheight的升序排列输出.


    首先Person.h文件

    #import <Foundation/Foundation.h>
    
    @interface Person : NSObject{
        NSString *_name;//姓名
        NSInteger _age;//年龄
        CGFloat _height;//身高
    }
    //初始化
    - (id)initWithName:(NSString *)name age:(NSInteger)age height:(CGFloat)height;
    //姓名
    - (void)setName:(NSString *)name;
    - (NSString *)name;
    
    //年龄
    - (void)setAge:(NSInteger)age;
    - (NSInteger)age;
    
    //身高
    - (void)setHeight:(CGFloat)height;
    - (CGFloat)height;
    //比較age
    - (NSComparisonResult)compareByAge:(Person *)anPerson;
    //比較身高
    - (NSComparisonResult)compareByHeight:(Person *)anPerson;
    
    //比較名字升序
    - (NSComparisonResult)compareByName:(Person *)anPerson;
    //名字降序
    - (NSComparisonResult)compareByNameDesceding:(Person *)anPerson;
    

    Person.m文件

    #import "Person.h"
    
    @implementation Person
    #pragma mark - setter and getter
    
    //set姓名
    - (void)setName:(NSString *)name{
        _name = name;
    }
    - (NSString *)name{
        return _name;
    }
    
    //set年龄
    - (void)setAge:(NSInteger)age{
        _age = age;
    }
    - (NSInteger)age{
       return _age;
    }
    
    //set身高
    - (void)setHeight:(CGFloat)height{
        _height = height;
    }
    - (CGFloat)height{
        return _height;
    }
    #pragma mark - init method
    //初始化
    - (id)initWithName:(NSString *)name age:(NSInteger)age height:(CGFloat)height{
        self = [super init];
        if (self != nil) {
            _name = name;
            _age = age;
            _height = height;
        }
        return self;
    }
    #pragma mark - override method
    //重写description
    - (NSString *)description{
        return [NSString stringWithFormat:@"name:%@ age:%ld, height:%g", _name, _age, _height];
    }
    #pragma mark - compare
    //比較年龄age
    - (NSComparisonResult)compareByAge:(Person *)anPerson{
        if ([self age] > [anPerson age]) {
            return NSOrderedDescending;//1 降序
        }else if([self age] == [anPerson age]){  //相等
            return NSOrderedSame;//0
        }else{
            return NSOrderedAscending;//-1  升序
        }
    }
    //比較身高
    - (NSComparisonResult)compareByHeight:(Person *)anPerson{
        if ([self height] > [anPerson height]) {
            return NSOrderedDescending;
        }else if([self height] == [anPerson height]){
            return NSOrderedSame;
        }else{
            return NSOrderedAscending;
        }
    }
    //比較名字
    - (NSComparisonResult)compareByName:(Person *)anPerson{
        return [[self name] compare:[anPerson name]];
    }
    //名字降序
    - (NSComparisonResult)compareByNameDesceding:(Person *)anPerson{
        return -[[self name] compare:[anPerson name]];
    }
    @end
    

    主函数中main.m调用

    #import <Foundation/Foundation.h>
    #import "Person.h"
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
            
            Person *per1 = [[Person alloc] initWithName:@"xiaoshuai" age:12 height:178];
            Person *per2 = [[Person alloc] initWithName:@"xiaomeng" age:22 height:175];
            Person *per3 = [[Person alloc] initWithName:@"xiaoguangguang" age:20 height:180];
            Person *per4 = [[Person alloc] initWithName:@"dapeng" age:30 height:190];
            NSArray *perArr = [[NSArray alloc] initWithObjects:per1,per2,per3,per4, nil];
            //对数组按姓名升序排序
            NSArray *sortArrName = [perArr sortedArrayUsingSelector:@selector(compareByName:)];
            NSLog(@"%@", sortArrName);
            //对数组中年龄升序排序
            NSArray *sortArrAge = [perArr sortedArrayUsingSelector:@selector(compareByAge:)];
            NSLog(@"%@", sortArrAge);
            //对数组中身高按升序排序
            NSArray *sortArrHeight = [perArr sortedArrayUsingSelector:@selector(compareByHeight:)];
            NSLog(@"%@", sortArrHeight);
            //名字降序
            NSArray *sortArrNameDescending = [perArr sortedArrayUsingSelector:@selector(compareByNameDesceding:)];
            NSLog(@"%@", sortArrNameDescending);
    
         }
        return 0;
    }


    測试结果:

    2014-08-14 22:03:37.650 Lesson_Sort[4908:303] (

        "name:dapeng age:30, height:190",

        "name:xiaoguangguang age:20, height:180",

        "name:xiaomeng age:22, height:175",

        "name:xiaoshuai age:12, height:178"

    )

    2014-08-14 22:03:37.651 Lesson_Sort[4908:303] (

        "name:xiaoshuai age:12, height:178",

        "name:xiaoguangguang age:20, height:180",

        "name:xiaomeng age:22, height:175",

        "name:dapeng age:30, height:190"

    )

    2014-08-14 22:03:37.652 Lesson_Sort[4908:303] (

        "name:xiaomeng age:22, height:175",

        "name:xiaoshuai age:12, height:178",

        "name:xiaoguangguang age:20, height:180",

        "name:dapeng age:30, height:190"

    )

    2014-08-14 22:03:37.652 Lesson_Sort[4908:303] (

        "name:xiaoshuai age:12, height:178",

        "name:xiaomeng age:22, height:175",

        "name:xiaoguangguang age:20, height:180",

        "name:dapeng age:30, height:190"

    )


  • 相关阅读:
    大龄程序员肉翻记录【一】
    spring boot 在不同环境下读取不同配置文件的一种方式
    JDK的一个关于stack的小bug
    《DISTRIBUTED SYSTEMS Concepts and Design》读书笔记 一
    一个简单的任务执行引擎设计
    spring集成guava的event bus
    Unity输出文件在itunes读取
    MaxScript调用DotNet时命名空间的问题
    max渲染通道元素的范例
    Unity里vertexShader里压扁模型来实现比较low的阴影
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5152988.html
Copyright © 2020-2023  润新知