// 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