NSArray数组内容排序

来源:互联网 发布:java下载32位 编辑:程序博客网 时间:2024/06/05 16:46

本文正对数组内容为其他类对象和字符串时,进行了排序操作。具体看看下面代码:

DeviceInfo.h

@interface DeviceInfo : NSObject@property (nonatomic, assign) NSInteger sequenceID;@property (nonatomic, strong) NSString *deviceID;@property (nonatomic, strong) NSString *deviceName;@end

DeviceInfo.m

#import "DeviceInfo.h"@implementation DeviceInfo@end


NSArrayCompareTest.h"

#import <Foundation/Foundation.h>@interface NSArrayCompareTest : NSObject-(void)testCompare;@end



NSArrayCompareTest.m"
@implementation NSArrayCompareTest-(id)init{    self=[super init];    if(self){        }    return  self;}-(void)testCompare{    DeviceInfo *deviceInfo1 = [[DeviceInfo alloc] init];    deviceInfo1.sequenceID=1;    deviceInfo1.deviceID=@"deviceID_1";    deviceInfo1.deviceName=@"deviceName_1";       DeviceInfo *deviceInfo2 = [[DeviceInfo alloc] init];    deviceInfo2.sequenceID=2;    deviceInfo2.deviceID=@"deviceID_2";    deviceInfo2.deviceName=@"deviceName_2";        DeviceInfo *deviceInfo3 = [[DeviceInfo alloc] init];    deviceInfo3.sequenceID=3;    deviceInfo3.deviceID=@"deviceID_3";    deviceInfo3.deviceName=@"deviceName_3";        NSMutableArray *array = [NSMutableArray arrayWithObjects:deviceInfo1, deviceInfo2, deviceInfo3,nil];        NSSortDescriptor *sortDescriptor1 = [NSSortDescriptor sortDescriptorWithKey:@"sequenceID" ascending:YES];    NSSortDescriptor *sortDescriptor2 = [NSSortDescriptor sortDescriptorWithKey:@"deviceID" ascending:YES];        NSArray *tempArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil]];        for(NSInteger i = 0; i < [tempArray count]; i++)    {        NSLog(@"%ld--------%@\n", [[tempArray objectAtIndex:i] sequenceID], [[tempArray objectAtIndex:i] deviceName]);    }        //对字符串数组进行排序    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];    //排序前    NSMutableString *outputBefore = [[NSMutableString alloc] init];    for(NSString *str in sortArray){        [outputBefore appendFormat:str];    }    NSLog(@"排序前:%@\n",outputBefore);    for(NSInteger i = 0; i < [sortArray count]; i++)    {        NSLog(@"%@", [sortArray objectAtIndex:i] );    }        //第一种排序    NSArray *stringArray = [sortArray sortedArrayUsingComparator:cmptr];    NSMutableString *outputAfter = [[NSMutableString alloc] init];    for(NSString *str in stringArray){       [outputAfter appendFormat:str];       }     NSLog(@"排序后:%@\n",outputAfter);    for(NSInteger i = 0; i < [sortArray count]; i++)    {        NSLog(@"%@", [sortArray objectAtIndex:i] );    }    }@end

运行代码:

NSLog(@"-------------NSArray_Sort_start--------------");    NSArrayCompareTest *sortTEST=[[NSArrayCompareTest alloc] init];    [sortTEST testCompare];    NSLog(@"-------------NSArray_Sort_end--------------");


结果:

2015-02-13 11:32:58.918 IOSStudy-01[5119:69341] -------------NSArray_Sort_start--------------2015-02-13 11:32:58.919 IOSStudy-01[5119:69341] 1--------deviceName_12015-02-13 11:32:58.919 IOSStudy-01[5119:69341] 2--------deviceName_22015-02-13 11:32:58.919 IOSStudy-01[5119:69341] 3--------deviceName_32015-02-13 11:32:58.919 IOSStudy-01[5119:69341] 排序前:1347826513151220282015-02-13 11:32:58.919 IOSStudy-01[5119:69341] 12015-02-13 11:32:58.919 IOSStudy-01[5119:69341] 32015-02-13 11:32:58.919 IOSStudy-01[5119:69341] 42015-02-13 11:32:58.919 IOSStudy-01[5119:69341] 72015-02-13 11:32:58.919 IOSStudy-01[5119:69341] 82015-02-13 11:32:58.919 IOSStudy-01[5119:69341] 22015-02-13 11:32:58.919 IOSStudy-01[5119:69341] 62015-02-13 11:32:58.919 IOSStudy-01[5119:69341] 52015-02-13 11:32:58.919 IOSStudy-01[5119:69341] 132015-02-13 11:32:58.919 IOSStudy-01[5119:69341] 152015-02-13 11:32:58.919 IOSStudy-01[5119:69341] 122015-02-13 11:32:58.919 IOSStudy-01[5119:69341] 202015-02-13 11:32:58.920 IOSStudy-01[5119:69341] 282015-02-13 11:32:58.920 IOSStudy-01[5119:69341] 排序后:1234567812131520282015-02-13 11:32:58.920 IOSStudy-01[5119:69341] 12015-02-13 11:32:58.923 IOSStudy-01[5119:69341] 32015-02-13 11:32:58.923 IOSStudy-01[5119:69341] 42015-02-13 11:32:58.923 IOSStudy-01[5119:69341] 72015-02-13 11:32:58.923 IOSStudy-01[5119:69341] 82015-02-13 11:32:58.923 IOSStudy-01[5119:69341] 22015-02-13 11:32:58.923 IOSStudy-01[5119:69341] 62015-02-13 11:32:58.924 IOSStudy-01[5119:69341] 52015-02-13 11:32:58.924 IOSStudy-01[5119:69341] 132015-02-13 11:32:58.924 IOSStudy-01[5119:69341] 152015-02-13 11:32:58.924 IOSStudy-01[5119:69341] 122015-02-13 11:32:58.924 IOSStudy-01[5119:69341] 202015-02-13 11:32:58.924 IOSStudy-01[5119:69341] 282015-02-13 11:32:58.925 IOSStudy-01[5119:69341] -------------NSArray_Sort_end--------------



0 0
原创粉丝点击