本文正對陣列內容為其他類物件和字串時,進行了排序操作。具體看看下面**:
deviceinfo.h
@inte***ce 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 @inte***ce nsarraycomparetest : nsobject
-(void)testcompare;
@end
nsarraycomparetest.m"
@implementation nsarraycomparetest
-(id)init
return self;
}-(void)testcompare
//對字串陣列進行排序
nscomparator cmptr = ^(id obj1, id obj2)
if ([obj1 integervalue] < [obj2 integervalue])
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)
nslog(@"排序前:%@\n",outputbefore);
for(nsinteger i = 0; i < [sortarray count]; i++)
//第一種排序
nsarray *stringarray = [sortarray sortedarrayusingcomparator:cmptr];
nsmutablestring *outputafter = [[nsmutablestring alloc] init];
for(nsstring *str in stringarray)
nslog(@"排序後:%@\n",outputafter);
for(nsinteger i = 0; i < [sortarray count]; 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_1
2015-02-13 11:32:58.919 iosstudy-01[5119:69341] 2--------devicename_2
2015-02-13 11:32:58.919 iosstudy-01[5119:69341] 3--------devicename_3
2015-02-13 11:32:58.919 iosstudy-01[5119:69341] 排序前:134782651315122028
2015-02-13 11:32:58.919 iosstudy-01[5119:69341] 1
2015-02-13 11:32:58.919 iosstudy-01[5119:69341] 3
2015-02-13 11:32:58.919 iosstudy-01[5119:69341] 4
2015-02-13 11:32:58.919 iosstudy-01[5119:69341] 7
2015-02-13 11:32:58.919 iosstudy-01[5119:69341] 8
2015-02-13 11:32:58.919 iosstudy-01[5119:69341] 2
2015-02-13 11:32:58.919 iosstudy-01[5119:69341] 6
2015-02-13 11:32:58.919 iosstudy-01[5119:69341] 5
2015-02-13 11:32:58.919 iosstudy-01[5119:69341] 13
2015-02-13 11:32:58.919 iosstudy-01[5119:69341] 15
2015-02-13 11:32:58.919 iosstudy-01[5119:69341] 12
2015-02-13 11:32:58.919 iosstudy-01[5119:69341] 20
2015-02-13 11:32:58.920 iosstudy-01[5119:69341] 28
2015-02-13 11:32:58.920 iosstudy-01[5119:69341] 排序後:123456781213152028
2015-02-13 11:32:58.920 iosstudy-01[5119:69341] 1
2015-02-13 11:32:58.923 iosstudy-01[5119:69341] 3
2015-02-13 11:32:58.923 iosstudy-01[5119:69341] 4
2015-02-13 11:32:58.923 iosstudy-01[5119:69341] 7
2015-02-13 11:32:58.923 iosstudy-01[5119:69341] 8
2015-02-13 11:32:58.923 iosstudy-01[5119:69341] 2
2015-02-13 11:32:58.923 iosstudy-01[5119:69341] 6
2015-02-13 11:32:58.924 iosstudy-01[5119:69341] 5
2015-02-13 11:32:58.924 iosstudy-01[5119:69341] 13
2015-02-13 11:32:58.924 iosstudy-01[5119:69341] 15
2015-02-13 11:32:58.924 iosstudy-01[5119:69341] 12
2015-02-13 11:32:58.924 iosstudy-01[5119:69341] 20
2015-02-13 11:32:58.924 iosstudy-01[5119:69341] 28
2015-02-13 11:32:58.925 iosstudy-01[5119:69341] -------------nsarray_sort_end--------------
NSArray 陣列排序
方法1,使用自帶的比較器 compare是陣列自帶的比較方法 nsarray array nsarrayarraywithobjects 3 1 2 nil nsarray array2 array sortedarrayusingselector selector compare nslog ar...
NSArray陣列隨機排序
nsarray和nsmutablearray的區別是前者是不可變陣列,一旦陣列初始化完成以後,就只能對陣列進行查詢操作,而後者是可變陣列,陣列初始化完成以後,繼而可以進行增 刪 改 查操作。所以對於陣列的亂序排序,必須要在可變陣列中進行操作。具體 如下 陣列隨機排序 nsmutablearray r...
NSArray方法 排序
nsarray用來儲存物件的有序列表,可以放入任何型別的物件,但它有兩個限制 首先,它只能儲存oc的物件,不能儲存c中的基本資料型別,如 int float enum struct 和隨機指標 其次不能在nsarray中儲存nil。建立陣列 nsarray array1 一年級 二年級 三年級 四年...