Thursday, 15 January 2015

ios - How to sort array of objects with ints in them in objective-c -



ios - How to sort array of objects with ints in them in objective-c -

i have array of powders i'd sort purchase how many pounds of them have

each powder object properties 1 of them beingness powderquantity

@interface powderobject : nsobject <nscoding> @property (nonatomic, retain) nsstring *powdername; //powder quantity in pounds @property (nonatomic, assign) double powderquantity; @property (nonatomic, assign) int numberid; @property (nonatomic, retain) nsstring *description; @end

that header object have been storing them in array by

nsmutablearray *arrayofpowders = [[powderdataclass listpowders] mutablecopy];

that line returns of powders have, , after them i'd sort them weight left of them (powderquantity). highest goes first , to the lowest degree goes last. i've tried using loops, have found them inefficient, , haven't got them work properly.

thanks help in advance

this doable nsarray. we'll using nssortdescriptors accomplish this, uses key-value coding on array of properties sort.

// note: key "powderquantity" must exact spelling of objects' property nssortdescriptor *sortbyquantity = [nssortdescriptor sortdescriptorwithkey:@"powderquantity" ascending:yes]; nsarray *sortdescriptors = @[sortbyquantity]; nsarray *sortedpowder = [arrayofpowders sortedarrayusingdescriptors:sortdescriptors];

note can add together more descriptors sortdescriptors secondary/tertiary/... sorting when 2 quantities equal.

ios objective-c sorting object

No comments:

Post a Comment