-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHRContactDataUtil.m
85 lines (76 loc) · 2.48 KB
/
HRContactDataUtil.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// HRContactDataUtil.m
// HRManger
//
// Created by goofygao on 8/6/16.
// Copyright © 2016 goofyy. All rights reserved.
//
#import "HRContactDataUtil.h"
#import "HRContactModel.h"
@implementation HRContactDataUtil
+ (NSMutableArray *) friendListDataBy:(NSMutableArray *)array{
NSMutableArray *ans = [[NSMutableArray alloc] init];
NSArray *serializeArray = [(NSArray *)array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {//排序
int i;
NSString *strA = ((HRContactModel *)obj1).pinyin;
NSString *strB = ((HRContactModel *)obj2).pinyin;
for (i = 0; i < strA.length && i < strB.length; i ++) {
char a = [strA characterAtIndex:i];
char b = [strB characterAtIndex:i];
if (a > b) {
return (NSComparisonResult)NSOrderedDescending;//上升
}
else if (a < b) {
return (NSComparisonResult)NSOrderedAscending;//下降
}
}
if (strA.length > strB.length) {
return (NSComparisonResult)NSOrderedDescending;
}else if (strA.length < strB.length){
return (NSComparisonResult)NSOrderedAscending;
}else{
return (NSComparisonResult)NSOrderedSame;
}
}];
char lastC = '1';
NSMutableArray *data;
NSMutableArray *oth = [[NSMutableArray alloc] init];
for (HRContactModel *user in serializeArray) {
char c = [user.pinyin characterAtIndex:0];
if (!isalpha(c)) {
[oth addObject:user];
}
else if (c != lastC){
lastC = c;
if (data && data.count > 0) {
[ans addObject:data];
}
data = [[NSMutableArray alloc] init];
[data addObject:user];
}
else {
[data addObject:user];
}
}
if (data && data.count > 0) {
[ans addObject:data];
}
if (oth.count > 0) {
[ans addObject:oth];
}
return ans;
}
+ (NSMutableArray *)friendListSectionBy:(NSMutableArray *)array{
NSMutableArray *section = [[NSMutableArray alloc] init];
[section addObject:UITableViewIndexSearch];
for (NSArray *item in array) {
HRContactModel *user = [item objectAtIndex:0];
char c = [user.pinyin characterAtIndex:0];
if (!isalpha(c)) {
c = '#';
}
[section addObject:[NSString stringWithFormat:@"%c", toupper(c)]];
}
return section;
}
@end