reference from :http://mobile.51cto.com/hot-434804.htm

1.构建Person类

Person.h

@interface Person : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *surname;
@property (nonatomic, strong) NSDate *dateOfBirth;
@end

2. 初始化ObjSort对象排序功能,包含一个NSArray数组,数组元素为自定义Person

ObjSort.h

/**
* 对NSArray自定义对象排序类
*/
@interface ObjSort : NSObject
-(void)personsSort_NSComparator;//NSComparator比较器
@end

ObjSort.m

@interface ObjSort()
@property (nonatomic, strong) NSMutableArray *personObjs;//存储了若干个Person类对象的数组
@end @implementation ObjSort /**
* 初始化对象
* 初始化NSMutableArray数组personObjs的初始内容, 包装Person类对象
*/
-(instancetype)init{
self = [super init];
if (self == nil) {
return nil;
} NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/yyyy"]; Person *person[5];
for(int i=0; i<5; i++){
person[i] = [[Person alloc] init];
} person[0].name = @"Smith";
person[0].surname = @"John";
person[0].dateOfBirth = [formatter dateFromString:@"03/01/1984"]; person[1].name = @"Andersen";
person[1].surname = @"Jane";
person[1].dateOfBirth = [formatter dateFromString:@"16/03/1979"]; person[2].name = @"Clark";
person[2].surname = @"Anne";
person[2].dateOfBirth = [formatter dateFromString:@"13/09/1995"]; person[3].name = @"Smith";
person[3].surname = @"David";
person[3].dateOfBirth = [formatter dateFromString:@"19/07/1981"]; person[4].name = @"Johnson";
person[4].surname = @"Rose";
person[4].dateOfBirth = [formatter dateFromString:@"22/02/1989"]; if (_personObjs==nil) {
_personObjs = [[NSMutableArray alloc] initWithCapacity:5];
}
for (int i=0; i<5; i++) {
[_personObjs addObject:person[i]];
} [self showPersons:_personObjs];
return self;
} /**
* 日志打印输出当前NSArray内容
*/
-(void)showPersons:(NSArray *)array{
Person *person; if (_personObjs == nil) {
NSLog(@"nil");
} for (int i =0; i< [array count]; i++) {
person = [array objectAtIndex:i];
if ([person isKindOfClass:[Person class]]) {
NSLog(@"---Person %d---", i);
NSLog(@"Name: %@", person.name);
NSLog(@"Surname: %@", person.surname);
NSLog(@"DateOfBirth: %@", person.dateOfBirth);
}
else{
NSLog(@"Object in personObjs is not Person class object.");
}
}
  NSLog(@"All person print over.\n\n");
}

方法一:NSComparator block + NSArray的 sortedArrayUsingSelector

在ObjSort中实现方法personsSort_NSComparator

/**
* 采用NSComparator比较的排序方法
*/
-(void)personsSort_NSComparator{ NSComparator sortBlock = ^(Person *p1 , Person *p2){
NSComparisonResult r1 = [p1.name compare:p2.name];
if(r1 != NSOrderedSame){
return r1;
}else {
NSComparisonResult r2 = [p1.surname compare:p2.surname];
if (r2 != NSOrderedSame)
return r2;
else
return [p1.dateOfBirth compare:p2.dateOfBirth];
}
};
NSArray *personList = [self.personObjs sortedArrayUsingComparator:sortBlock];
NSArray *personList = [self.personObjs sortedArrayUsingSelector:@selector(compare:)];
[self showPersons:personList];
}

方法二:利用对象的自定义方法compare:

Person.m中添加

-(NSComparisonResult)compare:(Person *)otherPerson{
NSComparisonResult r1 = [self.name compare:otherPerson.name];
if(r1 != NSOrderedSame){
return r1;
}else {
NSComparisonResult r2 = [self.surname compare:otherPerson.surname];
if (r2 != NSOrderedSame)
return r2;
else
return [self.dateOfBirth compare:otherPerson.dateOfBirth];
}
return [self.name compare:otherPerson.name];
}

ObjSort.m中添加

/**
* 采用自定义对象自定义函数compare进行NSArray排序
*/
-(void)personsSort_CustomCompare{
NSArray *personList = [self.personObjs sortedArrayUsingSelector:@selector(compare:)];
[self showPersons:personList];
}

在ObjSort.h中添加响应的接口

/**
* 对NSArray自定义对象排序类
*/
@interface ObjSort : NSObject
-(void)personsSort_NSComparator;//NSComparator比较器
-(void)personsSort_CustomCompare;//对象自定义比较方法Compare
@end

最后,在main.m(排序调用处)添加

        ObjSort *objSort = [[ObjSort alloc] init];
[objSort personsSort_NSComparator];

方法一结果

---Person ---
Name: Smith
Surname: John
DateOfBirth: -- :: +
---Person ---
Name: Andersen
Surname: Jane
DateOfBirth: -- :: +
---Person ---
Name: Clark
Surname: Anne
DateOfBirth: -- :: +
---Person ---
Name: Smith
Surname: David
DateOfBirth: -- :: +
---Person ---
Name: Johnson
Surname: Rose
DateOfBirth: -- :: +
All person print over. ---Person ---
Name: Andersen
Surname: Jane
DateOfBirth: -- :: +
---Person ---
Name: Clark
Surname: Anne
DateOfBirth: -- :: +
---Person ---
Name: Johnson
Surname: Rose
DateOfBirth: -- :: +
---Person ---
Name: Smith
Surname: David
DateOfBirth: -- :: +
---Person ---
Name: Smith
Surname: John
DateOfBirth: -- :: +
All person print over.

方法二结果

---Person ---
Name: Smith
Surname: John
DateOfBirth: -- :: +
---Person ---
Name: Andersen
Surname: Jane
DateOfBirth: -- :: +
---Person ---
Name: Clark
Surname: Anne
DateOfBirth: -- :: +
---Person ---
Name: Smith
Surname: David
DateOfBirth: -- :: +
---Person ---
Name: Johnson
Surname: Rose
DateOfBirth: -- :: +
All person print over. ---Person ---
Name: Andersen
Surname: Jane
DateOfBirth: -- :: +
---Person ---
Name: Clark
Surname: Anne
DateOfBirth: -- :: +
---Person ---
Name: Johnson
Surname: Rose
DateOfBirth: -- :: +
---Person ---
Name: Smith
Surname: David
DateOfBirth: -- :: +
---Person ---
Name: Smith
Surname: John
DateOfBirth: -- :: +
All person print over.

NSArary自定义对象排序 NSComparator, compare的更多相关文章

  1. ht-8 对arrayList中的自定义对象排序( Collections.sort(List<T> list, Comparator<? super T> c))

    package com.iotek.set; import java.util.ArrayList; import java.util.Collections; import java.util.Co ...

  2. Object-C中的排序和Compare陷阱

    来源:http://m.blog.csdn.net/blog/u011883764/38868097 Date : 2015-12-24 一.Compare陷阱 NSString有多个compare相 ...

  3. Java基础知识强化之集合框架笔记69:Collections类之ArrayList存储自自定义对象并排序的案例

    1. ArrayList存储自自定义对象并排序的案例: ArrayList存储自自定义对象,并使用Collections对ArrayList存储基本包装类的元素排序. 2. 代码实现: (1)Stud ...

  4. Java:集合,对列表(List)中的自定义对象按属性(字段)排序(正序、倒序)的方法

    1. 要求 对列表(List)中的自定义对象,要求能够按照对象的属性(字段)进行排序(正序.倒序). 如:用户对象(Member)有用户名(username).级别(level).出生日期(birth ...

  5. TreeSet 比较器排序 自定义对象

    package cn.itcast.day21.treeset2; import java.util.Comparator; import java.util.TreeSet; /* * TreeSe ...

  6. TreeMap 底层是红黑树 排序是根据key值进行的 添加元素时异常 Comparable异常 Comparator比较自定义对象放在键的位置

    package com.swift; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; ...

  7. Java基础知识强化之集合框架笔记46:Set集合之TreeSet存储自定义对象并遍历练习2(自然排序:Comparable)

    1. TreeSet存储自定义对象并遍历练习2: (1)Student.java package cn.itcast_06; /* * 如果一个类的元素要想能够进行自然排序,就必须实现自然排序接口 * ...

  8. Java基础知识强化之集合框架笔记45:Set集合之TreeSet存储自定义对象并遍历练习1(自然排序:Comparable)

    1. 自然排序: TreeSet会调用集合元素的compareTo(Object obj)方法来比较元素之间的大小关系,然后将集合元素按照升序排列,这种方式就是自然排序. Java中提供了一个Comp ...

  9. 《java入门第一季》之TreeSet存储自定义对象并保证排序和唯一

    上一篇用一个简单的例子,介绍了treeset集合存储的内部过程,这里再完善其存储自定义对象保证唯一. 需求:A:  * 自然排序,按照年龄从小到大排序  *         B:  * 成员变量值都相 ...

随机推荐

  1. Educational Codeforces Round 80 C. Two Arrays(组合数快速取模)

    You are given two integers nn and mm . Calculate the number of pairs of arrays (a,b)(a,b) such that: ...

  2. ➡️➡️➡️IELTS reading by Simon on Bili

    高分必备 雅思考官Simon手把手教你做阅读 p1 https://www.bilibili.com/video/av40131278?p=2 p2 https://www.bilibili.com/ ...

  3. 【PAT甲级】1041 Be Unique (20 分)(多重集)

    题意: 输入一个正整数N(<=1e5),接下来输入N个正整数.输出第一个独特的数(N个数中没有第二个和他相等的),如果没有这样的数就输出"None". AAAAAccepte ...

  4. 【JavaWeb】导入Excel并进行校验

    一.需要实现的目标 1.界面编写 2.导入表读取表名,进行校验,后台匹配(判断此表的名称是否能够模糊匹配上) 3.确定表存在,读取其中的数据,暂存 4.正则表达式数据校验(判断是否已存在,数据是否符合 ...

  5. RDLC 表达式设置精度

    =IIf(RTrim(Parameters!u_currency.Value)="VND","F0","F2")

  6. RestTemplate HttpMessageConverter报错的解决方案no suitable HttpMessageConverter

    错误 no suitable HttpMessageConverter found for response type and content type [text/html;charset=UTF- ...

  7. CSS - 表格细线边框

    通过 border-collapse: collapse cellpadding="0", cellspacing= "0" 实现 <!DOCTYPE h ...

  8. Python 爬取 热词并进行分类数据分析-[热词分类+目录生成]

    日期:2020.02.04 博客期:143 星期二   [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[ ...

  9. 泛型和Object的区别?

    泛型声明 public <T> T doSomeThing(T t){ return t; } Object声明 public Object doSomeThing(Object obj) ...

  10. 服务器settings

    1,如果增加了一个新的APP, 那么需要在服务器上 vim settings文件进行修改, 修改方法 i, :wq 2,正式服务器需要一样的操作