iOS中的排序笔记

NSComparator

NSComparator有4种枚举类型

NSOrderedDescending 降序,但是用他可以实现升序或者降序都没问题。

NSOrderedAscending 升序,但是目前没有使用出任何效果。。

NSOrderedSame 相同,等价于不改变吧?


NSSortDescriptor

这个排序可以直接对对象进行排序。用起来也很方便。

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"recommendTime" ascending:NO];

    NSMutableArray *sorts = [[NSMutableArray alloc] initWithObjects:&sort count:1];

    NSArray *sortArray = [_focusList sortedArrayUsingDescriptors:sorts];

recommendTime是一个时间,ascending,是表示是升序还是降序。



如果有相关资料blog,非常希望能提供我参考一下。感谢。


以下有其他资料来自网上,自己认为比较好的博文

计算时间差的方法:

CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();CFAbsoluteTime end= CFAbsoluteTimeGetCurrent();NSLog(@"time cost: %0.3f ms", (end - start)*1000);

使用NSComparator排序

comparator的定义如下所示:

typedef NSComparisonResult (^NSComparator)(id obj1, id obj2); 

上面的参数(obj1、obj2)就是我们将要做比较的对象。block返回的结果为NSComparisonResult类型来表示两个对象的顺序。

对上述的无序array的对象ID进行排序,代码如下:

NSArray *sortedArray = [unSortedArray sortedArrayUsingComparator:^(id obj1,id obj2)
{
   NSInteger val1 = ((Topic*)obj1).ID;
   NSInteger val2 = ((Topic*)obj2).ID;
  //升序,假如需要降序的话,只需要修改下面的逻辑
   if (val1 < val2)
  {
     return NSOrderedAscending;
  }
  else
  {
     return NSOrderedDescending;
  }
}

使用NSDescriptor排序

sort descriptor可以很方便的对数组进行多个key的排序。比如要对数组的对象先做ID排序,然后在对content进行排序的话,可以写成:

NSSortDescriptor *firstDescriptor = [[NSSortDescriptor alloc] initWithKey:@"ID" ascending:YES]; NSSortDescriptor *secondDescriptor = [[NSSortDescriptor alloc] initWithKey:@"content" ascending:YES];NSArray *sortArray = [NSArray arrayWithObjects:firstDescriptor,secondDescriptor,nil];NSArray*sortedArray = [unSortedArray sortedArrayUsingDescriptors:sortArray];

使用函数排序

具体代码实现方式如下:

NSInteger customSort(id obj1, id obj2,void* context)
{
   Topic *topic1 = (Topic*)obj1;	Topic *topic2 = (Topic*)obj2;	NSInteger val1 = topic1.ID;	NSInteger val2 = topic2.ID;	if (val1 > val2) 
  {		return (NSComparisonResult)NSOrderedDescending;	}	if (val1 < val2) 
  {		return (NSComparisonResult)NSOrderedAscending;	}	return (NSComparisonResult)NSOrderedSame;} sortedArray = [array sortedArrayUsingFunction:customSort context:nil];

快速排序

快速排序我想大多数的人都听过,由于排序效率在同为O(N*logN)的几种排序方法中效率较高,因此我们也对比以一下快排的表现,下面是快排的代码:

void quickSort(NSMutableArray *array, NSInteger first, NSInteger last, NSComparator comparator) 
{	if (first >= last) return;	id pivot = array[(first + last) / 2];	NSInteger left = first;	NSInteger right = last;	while (left <= right) {		while (comparator(array[left], pivot) == NSOrderedAscending)			left++;		while (comparator(array[right], pivot) == NSOrderedDescending)			right--;		if (left <= right)			[array exchangeObjectAtIndex:left++ withObjectAtIndex:right--];	}	quickSort(array, first, right, comparator);	quickSort(array, left, last, comparator);}NSArray* sort(NSArray *unsorted, NSComparator comparator){	NSMutableArray *a = [NSMutableArray arrayWithArray:unsorted];	quickSort(a, 0, a.count - 1, comparator);return a;}sortedArray = sort(array, ^(id obj1, id obj2)					 {						 Topic *topic1 = (Topic*)obj1;						 Topic *topic2 = (Topic*)obj2;						 NSNumber *val1 =[NSNumber numberWithLong:topic1.ID];						 NSNumber *val2 = [NSNumber numberWithLong:topic2.ID];						 return [val1 compare:val2];					 });

结果对比

iPhone4:2014-10-17 13:51:31.980 Algorithm_test[9578:907] NSComparator sort time cost: 163.708ms2014-10-17 13:51:32.273 Algorithm_test[9578:907] NSSortDescriptor sort time cost: 291.293ms2014-10-17 13:51:32.559 Algorithm_test[9578:907] function sort time cost: 281.485ms2014-10-17 13:51:36.582 Algorithm_test[9578:907] quick sort time cost: 4013.582ms
iPhone5s:2014-10-17 14:02:59.323 Algorithm_test[2971:60b] NSComparator sort time cost: 19.238ms2014-10-17 14:02:59.348 Algorithm_test[2971:60b] NSSortDescriptor sort time cost: 24.183ms2014-10-17 14:02:59.380 Algorithm_test[2971:60b] function sort time cost: 31.967ms2014-10-17 14:02:59.468 Algorithm_test[2971:60b] quick sort time cost: 86.205ms

可以发现前3种系统自带的方法运行速度很快,即便是在4这种老机器排序10000个对象也不到1s的时间,可以看出苹果算法的优化还是挺好的,但是快排的表现却不尽如人意,至于5s机器上,上述的排序时间都在几十毫秒,几乎可以忽略不计。因此建议在需要排序的时候采用系统自带的方法,至于用哪个可以看情况自己选择。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值