天天看點

OBjective-C:在可變數組NSMutableArray中添加相同對象後,進行自定義的排序方式輸出

  以下為自定義的排序方式的實作

OBjective-C:在可變數組NSMutableArray中添加相同對象後,進行自定義的排序方式輸出
OBjective-C:在可變數組NSMutableArray中添加相同對象後,進行自定義的排序方式輸出

  主函數測試:

  

#import <Foundation/Foundation.h>

#import "Person+Compare.h"

#import "Person.h"

int main(int argc, const char * argv[])

{

    @autoreleasepool

    {

        NSMutableArray *personArray = [[NSMutableArray alloc]initWithCapacity:20];

        [personArray addObject:[Person personWithName:@"Tom" andAge:20]];

        [personArray addObject:[Person personWithName:@"Smith" andAge:21]];

        [personArray addObject:[Person personWithName:@"Jhon" andAge:19]];

        [personArray addObject:[Person personWithName:@"Bruth" andAge:24]];

        //按姓名排序

        NSMutableArray *sortedByName = [NSMutableArray arrayWithArray:[personArray sortedArrayUsingSelector:@selector(CompareByName:)]];

        NSLog(@"%@",sortedByName);

        //按年齡排序

        NSMutableArray *sortedByAge = [NSMutableArray arrayWithArray:[personArray sortedArrayUsingSelector:@selector(CompareByAge:)]];

        NSLog(@"%@",sortedByAge);

    }

    return 0;

}

  測試結果為:

OBjective-C:在可變數組NSMutableArray中添加相同對象後,進行自定義的排序方式輸出
OBjective-C:在可變數組NSMutableArray中添加相同對象後,進行自定義的排序方式輸出

程式猿神奇的手,每時每刻,這雙手都在改變着世界的互動方式!

本文轉自當天真遇到現實部落格園部落格,原文連結:http://www.cnblogs.com/XYQ-208910/p/4740195.html,如需轉載請自行聯系原作者

繼續閱讀