ios 数组排序

第一种:利用数组的sortedArrayUsingComparator调用 NSComparator 

示例:

obj1obj2指的是数组中的对象

//1.数组中存放的是字符

NSComparator cmptr = ^(id obj1, id obj2){
        if ([obj1 integerValue] > [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedDescending;
        }
        if ([obj1 integerValue] < [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    };

NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil];

NSLog(@"排序前:%@",sortArray);

NSArray *array = [sortArray sortedArrayUsingComparator:cmptr];

NSLog(@"排序后:%@",array);

//2.数组中存放的是字典对象

NSComparator cmptr = ^(id obj1,id obj2) {

if ([[obj1 objectForKey:@"date_update"] intValue] > [[obj2 objectForKey:@"date_update"] intValue]) {

return (NSComparisonResult)NSOrderedDescending;
        }
        if ([[obj1 objectForKey:@"date_update"] intValue] < [[obj2 objectForKey:@"date_update"] intValue]) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;

};

favoritesArray为NSArray数组,内容为:(

{

"date_update" = 1374823501;

id = 2;

videolink = "http://v.youku.com/player/getRealM3U8/vid/XNTY5NzcxNTAw/type//video.m3u8";

},

{

"date_update" = 1375177741;

id = 1;

videolink = "http://v.youku.com/player/getRealM3U8/vid/XNTY5MjcwNDcy/type//video.m3u8";

},

{

"date_update" = 1374824546;

id = 3;

videolink = "http://v.youku.com/player/getRealM3U8/vid/XNTY5Njk0Mjg0/type//video.m3u8";

}

)

favoritesArray = [favoritesArray sortedArrayUsingComparator:cmptr];

NSLog(@"排序后 array ===%@",favoritesArray);

第二种排序方法:利用sortedArrayUsingFunction 调用对应方法customSort。

这个方法中的obj1obj2分别是指数组中的对象。

NSInteger customSort(id obj1, id obj2, void *context)

{

if ([[obj1 objectForKey:@"date_update"] integerValue] > [[obj2 objectForKey:@"date_update"] integerValue]) {

return (NSComparisonResult)NSOrderedDescending;
        }
       
        if ([[obj1 objectForKey:@"date_update"] integerValue] < [[obj2 objectForKey:@"date_update"] integerValue]) {

return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;

}

favoritesArray = [favoritesArray sortedArrayUsingFunction:customSort context:nil];

//

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html

NSInteger intSort(id num1, id num2,void*context){

  int v1 =[num1 intValue];int v2 =[num2 intValue];

  if(v1 < v2)returnNSOrderedAscending;

  elseif(v1 > v2)returnNSOrderedDescending;

  elsereturnNSOrderedSame;
}
NSArray*sorted_bookings =[myUnsortedArray sortedArrayUsingFunction:Sort_Bookingdate_Comparer context:self];

NSIntegerSort_Bookingdate_Comparer(id id1, id id2,void*context){// Sort
   FunctionBooking* booking1 =(Booking*)id1;

  Booking* booking2 =(Booking*)id2;

   return([booking1.BOOKING_DATE compare:booking2.BOOKING_DATE]);
}

This I used to sort bookings by bookingdate. Booking is a class with a synthesized instance variable called BOOKING_DATE.

[opponentMatchDicts sortUsingFunction:compareMatchByDate context:nil];

static int compareMatchByDate( id m1, id m2,void*context){

  NSDictionary*mDict1 =(NSDictionary*) m1;
  NSDictionary*mDict2 =(NSDictionary*) m2;   NSDate*date1 =[mDict1 objectForKey:kMatchNSDate];
  NSDate*date2 =[mDict2 objectForKey:kMatchNSDate];
  int rv =[date1 compare:date2];
  return rv;
}

//

第三种利用sortUsingDescriptors调用NSSortDescriptor

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date_update" ascending:NO];

//其中,date_update为数组中的对象的属性,这个针对数组中存放对象比较更简洁方便.

//1.对数组中的字典对象排序

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date_update" ascending:YES];

[favoritesArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

NSLog(@"排序后  favoritesArray===%@",favoritesArray);

//2.可变数组

NSMutableArray  *tempArray = [[NSMutableArray alloc]init];

for (int i=0; i<10; i++) {

NSString *str = [NSString stringWithFormat:@"%d",i];

NSMutableDictionary *httpHeaderDic = [[NSMutableDictionary alloc]initWithCapacity:2];

[httpHeaderDic setValue:str forKey:@"id"];

[tempArray addObject:httpHeaderDic];

}

NSLog(@"排序前 Array ==%@",tempArray);

NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"id" ascending:NO];

[tempArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor2]];

NSLog(@"排序后 Array ==%@",tempArray);

上一篇:关于js中alert弹出窗口换行!


下一篇:百度地图js版定位控件