Coder Social home page Coder Social logo

summerhh / bmchinesesort Goto Github PK

View Code? Open in Web Editor NEW

This project forked from baymax0/bmchinesesort

0.0 2.0 0.0 371 KB

根据json返回的数组对象,对数组中某个对象的某个字段的值进行中文排序,并输出排序后的数组对象用于在tableview中显示

Objective-C 100.00%

bmchinesesort's Introduction

BMChineseSort

Use Language Use Language

介绍

BMChineseSort是一个为模型、字典、字符串数组根据特定中文属性基于tableview分组优化的工具类,基于异步、多线程降低排序时间。对于多音字的问题,开放了一个映射属性,可手动修改个别多音字或你想要的映射关系。

使用(TableView分组排序)

分组排序

普通自定义的模型对象

//Person模型
@interface Person : NSObject
@property (strong , nonatomic) NSString * name;
@property (assign , nonatomic) NSInteger number;
@end

使用sortWithArray方法进行排序

NSMutableArray<Person*> *dataArr;//数据源
NSMutableArray *firstLetterArray;//排序后的出现过的拼音首字母数组
NSMutableArray *sortedModelArr;//排序好的结果数组
[BMChineseSort sortWithArray:dataArr key:@"name" finish:^(bool isSuccess, NSMutableArray *unGroupArr, NSMutableArray *sectionTitleArr, NSMutableArray<NSMutableArray *> *sortedObjArr) {
        if (isSuccess) {
            self.firstLetterArray = sectionTitleArr;
            self.sortedModelArr = sortedObjArr;
            [_tableView reloadData];
        }
    }];

使用排序结果

    //section的titleHeader
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        return [self.indexArray objectAtIndex:section];
    }
    //section行数
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return [self.indexArray count];
    }
    //每组section个数
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return [[self.letterResultArr objectAtIndex:section] count];
    }
    //section右侧index数组
    -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
        return self.indexArray;
    }
    //点击右侧索引表项时调用 索引与section的对应关系
    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
        return index;
    }
    //返回cell
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
        if (cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
        }
        //获得对应的Person对象<替换为你自己的model对象>
        Person *p = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        cell.textLabel.text = p.name;
        return cell;
    }

字符串分组排序

使用方法与模型排序相同,注意 key = nil,否则排序失败。

NSMutableArray * provinceArr = @[@"北京",@"河南",@"重庆",@"沈阳",@"长春",@"abc",];
//字符串数组 key 传nil 即可
[BMChineseSort sortWithArray:provinceArr key:nil finish:^(bool isSuccess, NSMutableArray *unGroupArr, NSMutableArray *sectionTitleArr, NSMutableArray<NSMutableArray *> *sortedObjArr){
        // sortedObjArr是NSMutableArray<NSMutableArray *<NSString*>>类型
}];

排序 不分组

在回调方法中 拿到 unGroupArr即可 ,模型和字符串排序都支持

设置

通过 BMChineseSortSetting.share 来进行设置

拼音转换方式替换

两种方法都是基于多线程异步操作后进行优化了。

  • sortMode=1 使用系统CFStringTransform 方法转换,比较耗时

  • sortMode=2 使用汉字码表对应的首字母码表,通过编码顺序查找,比较快。(码表来源于网络,没有验证过,但基本没什么问题)如遇到多音字或者可能错误的拼音映射,可以码表配合polyphoneMapping手动修改错误的映射

BMChineseSortSetting.share.sortMode = 1

剔除不要特定字符开头的元素

如果想过滤 某些字符开头的元素,使用 ignoreModelWithPrefix,下面例子中剔除了所有元素中对应key的值是数字开头的元素

BMChineseSortSetting.share.ignoreModelWithPrefix = @"0123456789"

多音字映射

实际使用中如果遇到想手动映射拼音的 可以使用到polyphoneMapping完成映射,由于多音字在本地中是无法动态解决的,如果不是通过后台获取的拼音,则只能通过手动过滤的方法了。 如果你觉得常用的,可以在issue中提出来,我更新在默认值中。

  • 使用格式: {"匹配的文字":"对应的首字母(大写)"}
//使用时不需要添加,直接赋值即可,不会覆盖上一次的值
BMChineseSortSetting.share.polyphoneMapping = @{@"长安":@"CA"};
BMChineseSortSetting.share.polyphoneMapping = @{@"":@"C"};//所有长 都映射为chang(C)
BMChineseSortSetting.share.polyphoneMapping = @{@"厦门":@"XM",@"重庆":@"CQ"};

默认已添加的常用的多音字映射:

    @{  @"重庆":@"CQ",
        @"厦门":@"XM",
        @"":@"C",
        @"":@"S",
    }

Migration

Version 0.2.1

添加未分组的结果集回调,修改demo

Version 0.2.0

1.合并IndexWithArray:sortObjectArray方法,减少对数据的多次遍历造成的时间浪费,

2.同时添加将排序转入后台多线程,使用block回调拿到数据。

3.将模型与字符串排序合并为一个方法,使用通过key区分。

bmchinesesort's People

Contributors

baymax0 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.