Object-C 学习笔记 数组的排序
这里有一个 Student 类
//
// Student.h
// 可变字符串
//
// Created by game912 on 2018/12/28.
// Copyright © 2018年 john. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property(assign,nonatomic)NSInteger age;
@property(strong, nonatomic)NSString* name;
- (NSComparisonResult)compareStudent:(Student*) student;
@end
//
// Student.m
// 可变字符串
//
// Created by game912 on 2018/12/28.
// Copyright © 2018年 john. All rights reserved.
//
#import "Student.h"
@implementation Student
//重写描述器 格式化输出
- (NSString *)description
{
return [NSString stringWithFormat:@"name:%@, age:%ld", self.name,self.age];
}
//自定义比较方法
- (NSComparisonResult)compareStudent:(Student*) student{
if(self.age > student.age){
return NSOrderedDescending;
}else if(self.age < student.age){
return NSOrderedAscending;
}else{
//return NSOrderedSame; 年龄相等 按照 名字排序
return [self.name compare:student.name];
}
}
@end
-
普通数组的排序比较
这里主要是 字符串的比较 使用到了 它 : NSComparisonResult
///
///普通数组的比较
///
NSMutableArray* dataSource = [[NSMutableArray alloc]
initWithObjects:@"4",@"8",@"2",@"1",@"6",@"7",@"9",@"8",@"3",@"5",@"0", nil];
//比较的用法
NSComparisonResult resoult = [@"4" compare:@"5"];
switch (resoult) {
case NSOrderedSame:
NSLog(@"==");
break;
case NSOrderedAscending:
NSLog(@"<");
break;
case NSOrderedDescending:
NSLog(@">");
break;
}
//相当于 C# action 把一个方法 用参数去传递 “isEqualToString:” “:”也是方法的一部分
SEL action = @selector(compare:);
[dataSource sortUsingSelector:action];//使用aciton 方法进行排序
NSLog(@"dateSource:%@",dataSource); //结果的输出
-
普通数组多属性的比较
涉及到了 SEL studentCom = @selector(compareStudent:);
自定义 排序方法的 使用
///
///普通数组多属性的比较
///
NSMutableArray* students = [NSMutableArray new];
Student* student0 = [Student new];
Student* student1 = [Student new];
Student* student2 = [Student new];
Student* student3 = [Student new];
student0.age = 21;
student0.name =@"灵境";
student1.age = 29;
student1.name = @"类库";
student2.age = 29;
student2.name = @"类库Dad";
student3.age = 21;
student3.name = @"王大锤";
[students addObject:student0];
[students addObject:student1];
[students addObject:student2];
[students addObject:student3];
//SEL studentCom = @selector(compareStudent:);
//[students sortUsingSelector:studentCom];
// 自定义的 compareStudent 比较方法
[students sortUsingSelector:@selector(compareStudent:)];
NSLog(@"students:%@",students); //结果的输出
-
普通数组多属性的比较 描述器的使用
使用到了 NSSortDescriptor* 描述器
///
///普通数组多属性的比较 描述器的使用
///
NSMutableArray* studentArray = [NSMutableArray new];
Student* student00 = [Student new];
Student* student11 = [Student new];
Student* student22 = [Student new];
Student* student33 = [Student new];
student00.age = 21;
student00.name =@"灵境";
student11.age = 29;
student11.name = @"类库";
student22.age = 29;
student22.name = @"类库Dad";
student33.age = 21;
student33.name = @"王大锤";
[studentArray addObject:student0];
[studentArray addObject:student1];
[studentArray addObject:student2];
[studentArray addObject:student3];
//ascend 升序
NSSortDescriptor* desAge = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO]; //age 降序 解释器
NSSortDescriptor* desNage = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; //name升序 解释器
[studentArray sortUsingDescriptors:@[desAge,desNage]];
NSLog(@"students:%@",studentArray); //结果的输出
//
// main.m
// 可变字符串
//
// Created by game912 on 2018/12/28.
// Copyright © 2018年 john. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
///
///普通数组的比较
///
NSMutableArray* dataSource = [[NSMutableArray alloc]
initWithObjects:@"4",@"8",@"2",@"1",@"6",@"7",@"9",@"8",@"3",@"5",@"0", nil];
//比较的用法
NSComparisonResult resoult = [@"4" compare:@"5"];
switch (resoult) {
case NSOrderedSame:
NSLog(@"==");
break;
case NSOrderedAscending:
NSLog(@"<");
break;
case NSOrderedDescending:
NSLog(@">");
break;
}
//相当于 C# action 把一个方法 用参数去传递 “isEqualToString:” “:”也是方法的一部分
SEL action = @selector(compare:);
[dataSource sortUsingSelector:action];//使用aciton 方法进行排序
NSLog(@"dateSource:%@",dataSource); //结果的输出
///
///普通数组多属性的比较
///
NSMutableArray* students = [NSMutableArray new];
Student* student0 = [Student new];
Student* student1 = [Student new];
Student* student2 = [Student new];
Student* student3 = [Student new];
student0.age = 21;
student0.name =@"灵境";
student1.age = 29;
student1.name = @"类库";
student2.age = 29;
student2.name = @"类库Dad";
student3.age = 21;
student3.name = @"王大锤";
[students addObject:student0];
[students addObject:student1];
[students addObject:student2];
[students addObject:student3];
//SEL studentCom = @selector(compareStudent:);
//[students sortUsingSelector:studentCom];
[students sortUsingSelector:@selector(compareStudent:)];
NSLog(@"students:%@",students); //结果的输出
///
///普通数组多属性的比较 描述器的使用
///
NSMutableArray* studentArray = [NSMutableArray new];
Student* student00 = [Student new];
Student* student11 = [Student new];
Student* student22 = [Student new];
Student* student33 = [Student new];
student00.age = 21;
student00.name =@"灵境";
student11.age = 29;
student11.name = @"类库";
student22.age = 29;
student22.name = @"类库Dad";
student33.age = 21;
student33.name = @"王大锤";
[studentArray addObject:student0];
[studentArray addObject:student1];
[studentArray addObject:student2];
[studentArray addObject:student3];
//ascend 升序
NSSortDescriptor* desAge = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO]; //age 降序 解释器
NSSortDescriptor* desNage = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; //name升序 解释器
[studentArray sortUsingDescriptors:@[desAge,desNage]];
NSLog(@"students:%@",studentArray); //结果的输出
}
return 0;
}