指点成金-最美分享吧

登录

[iOS功能]- iOS数组排序(倒叙 生序 降序)

佚名 举报

篇首语:本文由小编为大家整理,主要介绍了[iOS功能]- iOS数组排序(倒叙 生序 降序)相关的知识,希望对你有一定的参考价值。

参考技术A ios开发的过程中,经常需要使数组中的数据倒叙排列!比如在tableView显示数据的时候需要使数据倒序排列!那么如何解决数组的倒序排列问题呢?好多开发的小伙伴可能是便利数组的下标来获取,但是这种方法如果数据很大的情况下程序体验度会降低 , 这里介绍一个方法,一句话便可以搞定数组的倒序排序问题!

sortedArrayUsingSelector

按Key值大小对NSDictionary排序

sortedArrayUsingSelector

sortedArrayUsingComparator

sortedArrayUsingDescriptors & sortUsingDescriptors

前者带返回值,是NSArray的方法,排好序的数组是返回值中的数组;
后者不带返回值,是NSMutableArray的方法,是对当前数组自己排序
接下来根据一个对象的属性,排列这个对象

.h

.m

排序方法的实现

这里的NSArray中的第一元素表示首先按照这个元素的升序或者降序进行排序,对于有重复项的,再按照第二元素进行排序,依次进行类推

转自: https://www.jianshu.com/p/e9d561140f5b

iOS 数组排序

降序:(从大到小)

-(void)sortArrayDatas{    [array_datas sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {        RainfallStations *station_One = obj1;        RainfallStations *station_Two = obj2;                double totalRain_one = station_One.totalRain;        double totalRain_two = station_Two.totalRain;        if (totalRain_one>totalRain_two) {            return NSOrderedAscending;        }        else if (totalRain_one < totalRain_two){            return NSOrderedDescending;        }else{            return NSOrderedSame;        }    }];}

 

升序:(从小到大)

-(void)sortArrayDatas{    [array_datas sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {        RainfallStations *station_One = obj1;        RainfallStations *station_Two = obj2;                double totalRain_one = station_One.totalRain;        double totalRain_two = station_Two.totalRain;        if (totalRain_one>totalRain_two) {            return NSOrderedDescending;        }        else if (totalRain_one < totalRain_two){            return NSOrderedAscending;        }else{            return NSOrderedSame;        }    }];}

 

以上是关于[iOS功能]- iOS数组排序(倒叙 生序 降序)的主要内容,如果未能解决你的问题,请参考以下文章