NSArray数组的排序

main.m
 
//
// main.m
// NSArraySort
//
// Created by Rayln Guan on 9/4/13.
// Copyright (c) 2013 Rayln Guan. All rights reserved.
//

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

void arraySort1(){
NSArray *arr = [NSArray arrayWithObjects:@"1",@"3",@"2",@"0", nil];
//告诉Array用compare方法去比较 例如: [@"2" compare:@"3"]
NSArray *newArr = [arr sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"newArr%@", newArr);
}

void arraySort2(){
Student *stu1 = [Student student:@"guan" :@"ruilin"];
Student *stu2 = [Student student:@"a" :@"bc"];
Student *stu3 = [Student student:@"h" :@"ga"];
Student *stu4 = [Student student:@"a" :@"aa"];
NSArray *arr = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
NSArray *newArr = [arr sortedArrayUsingSelector:@selector(sort:)];
NSLog(@"%@:", newArr);
}
//使用block排序
void arraySort3(){
Student *stu1 = [Student student:@"guan" :@"ruilin"];
Student *stu2 = [Student student:@"a" :@"bc"];
Student *stu3 = [Student student:@"h" :@"ga"];
Student *stu4 = [Student student:@"a" :@"aa"];
NSArray *arr = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];

NSArray *newArr = [arr sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student *stu) {
NSComparisonResult result1 = [obj1.firstname compare:stu.firstname];
if(result1 == NSOrderedSame){
result1 = [obj1.lastname compare:stu.lastname];
}
return result1;
}];
NSLog(@"%@:", newArr);
}

//使用属性进行比较
void arraySort4(){
Student *stu1 = [Student student:@"guan" :@"ruilin" :@"book2"];
Student *stu2 = [Student student:@"a" :@"bc" :@"book1"];
Student *stu3 = [Student student:@"h" :@"ga" :@"book1"];
Student *stu4 = [Student student:@"a" :@"aa" :@"book3"];

NSArray *arr = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
//key中的值是写@property中的值!!切记,不是属性_firstname
NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
NSSortDescriptor *sort2 = [NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];
NSSortDescriptor *sort3 = [NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];
NSArray *newArr = [arr sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sort1,sort2,sort3,nil]];
NSLog(@"---description:%@", newArr);
}

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

@autoreleasepool {
arraySort1();
arraySort2();
arraySort3();
arraySort4();
}
return 0;
}




Student.h
//
//
// Student.h
// NSArraySort
//
// Created by Rayln Guan on 9/4/13.
// Copyright (c) 2013 Rayln Guan. All rights reserved.
//

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

@interface Student : NSObject

@property (nonatomic, retain) NSString *firstname;
@property (nonatomic, retain) NSString *lastname;
@property (nonatomic, retain) Book *book;

+ (Student *) student:(NSString *)firstname :(NSString *)lastname;
+ (Student *) student:(NSString *)firstname :(NSString *)lastname :(NSString *)book;
- (NSComparisonResult) sort:(Student *)stu;
@end



Student.m

//
// Student.m
// NSArraySort
//
// Created by Rayln Guan on 9/4/13.
// Copyright (c) 2013 Rayln Guan. All rights reserved.
//

#import "Student.h"

@implementation Student

+(Student *)student:(NSString *)fristname :(NSString *)lastname{
Student *stu = [[[Student alloc] init] autorelease];
stu.firstname = fristname;
stu.lastname = lastname;
return stu;
}

+(Student *)student:(NSString *)firstname :(NSString *)lastname :(NSString *)book{
Student *stu = [[[Student alloc] init] autorelease];
stu.firstname = firstname;
stu.lastname = lastname;
Book *bookAlloc = [[[Book alloc] init] autorelease];
bookAlloc.name = book;
stu.book = bookAlloc;
return stu;
}

//实现排序的方法!!!
-(NSComparisonResult)sort:(Student *)stu{
NSComparisonResult result1 = [self.firstname compare:stu.firstname];
if(result1 == NSOrderedSame){
result1 = [self.lastname compare:stu.lastname];
}
return result1;
}

-(void)dealloc{
//NSLog(@"Student has been distory!! %@", self.firstname);
[_firstname release];
[_lastname release];
[_book release];
[super dealloc];
}
- (NSString *)description{
return [NSString stringWithFormat:@"%@-%@--book:%@", _firstname, _lastname, _book.name];
}
@end




Book.h
//
// Book.h
// NSArraySort
//
// Created by Rayln Guan on 9/5/13.
// Copyright (c) 2013 Rayln Guan. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Book : NSObject

@property (nonatomic, retain) NSString *name;

@end



Book.m

//
// Book.m
// NSArraySort
//
// Created by Rayln Guan on 9/5/13.
// Copyright (c) 2013 Rayln Guan. All rights reserved.
//

#import "Book.h"

@implementation Book

- (void)dealloc{
NSLog(@"Book has been destory!! %@", self);
[super dealloc];
}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值