ios - sorting a nested NSMutableArray -
the nsmutablearray
want sort looks this:
( { "title" = "bags"; "price" = "$200"; }, { "title" = "watches"; "price" = "$40"; }, { "title" = "earrings"; "price" = "$1000"; } )
it's nsmutablearray
contain collection of nsmutablearray
s. want sort price
first title
.
nssortdescriptor *sortbyprices = [[nssortdescriptor alloc] initwithkey:@"price" ascending:yes]; nssortdescriptor *sortbytitle = [[nssortdescriptor alloc] initwithkey:@"title" ascending:yes]; [arrayproduct sortedarrayusingdescriptors:[nsarray arraywithobjects:sortbyprices,sortbytitle,nil]];
however, didn't seems work, how sort nested nsmutablearray
?
try
nsmutablearray *arrayproducts = [@[@{@"price":@"$200",@"title":@"bags"},@{@"price":@"$40",@"title":@"watches"},@{@"price":@"$1000",@"title":@"earrings"}] mutablecopy]; nssortdescriptor *pricedescriptor = [nssortdescriptor sortdescriptorwithkey:@"" ascending:yes comparator:^nscomparisonresult(nsdictionary *dict1, nsdictionary *dict2) { return [dict1[@"price"] compare:dict2[@"price"] options:nsnumericsearch]; }]; nssortdescriptor *titledescriptor = [nssortdescriptor sortdescriptorwithkey:@"title" ascending:yes]; [arrayproducts sortusingdescriptors:@[pricedescriptor,titledescriptor]]; nslog(@"sortedarray : %@",arrayproducts);
Comments
Post a Comment