123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- //
- // LCPinYinSortHelper.m
- // LiveChat
- //
- // Created by 张灿 on 2018/4/15.
- // Copyright © 2018年 caicai. All rights reserved.
- //
- #import "LCPinYinSortHelper.h"
- @implementation LCPinYinSortHelper
- + (NSMutableArray *)getSearchFullPinyinBY:(NSString*)searhName{
- NSMutableArray *ans = [[NSMutableArray alloc] init];
- NSMutableArray* data = [NSMutableArray arrayWithArray:[LCSaveData getCityDict].allValues];
- for (NSString* str in data) {
- if ([[self getFullPinyin:str] hasPrefix:[self getFullPinyin:searhName]]) {
- [ans addObject:str];
- }else if([[self getShortPinyin:str] hasPrefix:[self getFullPinyin:searhName]]){
- [ans addObject:str];
- }
- }
- NSArray *serializeArray = [(NSArray *)ans sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {//排序
- int i;
- NSString *strA = ((NSString *)obj1).fullPinyin;
- NSString *strB = ((NSString *)obj2).fullPinyin;
- 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;
- }
- }];
-
-
- return [NSMutableArray arrayWithArray:serializeArray];
-
- }
- + (NSString*)getShortPinyin:(NSString*)str{
- NSMutableString *shortSpelling = [[NSMutableString alloc]init];
- for (NSInteger i = 0; i < [str length]; i++)
- {
- NSString *word = [str substringWithRange:NSMakeRange(i, 1)];
- NSString *pinyin = word.fullPinyin;
-
- if ([pinyin length])
- {
- [shortSpelling appendString:[pinyin substringToIndex:1]];
- }
- }
- return shortSpelling;
- }
- + (NSString*)getFullPinyin:(NSString*)str{
- NSMutableString *fullSpelling = [[NSMutableString alloc]init];
- for (NSInteger i = 0; i < [str length]; i++)
- {
- NSString *word = [str substringWithRange:NSMakeRange(i, 1)];
- NSString *pinyin = word.fullPinyin;
-
- if ([pinyin length])
- {
- [fullSpelling appendString:pinyin];
- }
- }
- return fullSpelling;
- }
- + (NSMutableArray *) getCityListDataBy:(NSMutableArray *)array{
- NSMutableArray *ans = [[NSMutableArray alloc] init];
-
- NSArray *serializeArray = [(NSArray *)array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {//排序
- int i;
- NSString *strA = ((NSString *)obj1).pinYin;
- NSString *strB = ((NSString *)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 (NSString *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 *)getCityListSectionBy:(NSMutableArray *)array{
- NSMutableArray *section = [[NSMutableArray alloc] init];
- [section addObject:@"定位"];
- // [section addObject:UITableViewIndexSearch];
- for (NSArray *item in array) {
- NSString *user = [item objectAtIndex:0];
- char c = [user.pinYin characterAtIndex:0];
- // if (!isalpha(c)) {
- // [section addObject:@"定位"];
- // }
- [section addObject:[NSString stringWithFormat:@"%c", toupper(c)]];
- }
- return section;
- }
- @end
|