i have nsmutablearray(arrayone) structured so..
({    "item_image_timestamp" = "492364855.234597";    "item_image_url" = "sample url";    "item_image_vote" = 123; }, {    "item_image_timestamp" = "492364458.236597";    "item_image_url" = "sample url";    "item_image_vote" = 456; }, {    "item_image_timestamp" = "492364179.052397";    "item_image_url" = "sample url";    "item_image_vote" = 6184; }, {    "item_image_timestamp" = "492364789.004447";    "item_image_url" = "sample url";    "item_image_vote" = 64; }, {    "item_image_timestamp" = "492364356.002341";    "item_image_url" = "sample url";    "item_image_vote" = 3778; })   the maximum number of objects can contained in arrayone 10.
then, have second nsmutablearray(arraytwo) structured arrayone
({    "item_image_timestamp" = "492364855.234597";    "item_image_url" = "sample url";    "item_image_vote" = 123; }, {    "item_image_timestamp" = "492364458.236597";    "item_image_url" = "sample url";    "item_image_vote" = 456; })   ..except maximum number of objects can contained in arraytwo 3.
now, want is..
- add objects of arraytwo arrayone (keeping in mind arrayone can hold maximum of 10 objects)
 - keep top 5 objects sorted key "item_image_vote" (top 5 voted object should not replaced)
 - if objects in arrayone need replaced, item lowest value key "item_image_timestamp" should replaced first.(oldest object replaced first..followed the second oldest).
 
i hope did not make question confusing. thank in advance.
adding, removing , measuring length methods on mutable arrays, straight-forward apply.  interesting part of question sort criteria, addressed nssortdescriptor.
nsarray allows application of these in groups sortedarrayusingdescriptors (notice plural).  approach add arrays, sort according criteria , truncate max length.
// sort on vote, descending nssortdescriptor *votedescriptor = [nssortdescriptor sortdescriptorwithkey:@"item_image_vote" ascending:no];  // sort on date, descending nssortdescriptor *datedescriptor = [nssortdescriptor sortdescriptorwithkey:@"item_image_timestamp" ascending:no];  nsmutablearray *bigarray = [arrayone mutablecopy];  // note: lowercase variable names considered preferable style [bigarray addobjectsfromarray:arraytwo];  [bigarray sortusingdescriptors:@[votedescriptor, datedescriptor]]; // note: order matters. want votes sorted first, ties broken date   the result bigarray, sorted, truncated max size.
nsarray *result = [bigarray subarraywithrange:nsmakerange(0, min(10, bigarray.count))];      
Comments
Post a Comment