0
votes

I have an NSMutableArray(ArrayOne) structured like 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 that can be contained in ArrayOne is 10.

Then, I have a second NSMutableArray(ArrayTwo) structured just like 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 that the maximum number of objects that can be contained in ArrayTwo is 3.

Now, What I want to do is..

  • add objects of ArrayTwo into ArrayOne (keeping in mind that ArrayOne can only hold a maximum of 10 objects)
  • keep the top 5 objects sorted by the key "item_image_vote" (top five voted object should not be replaced)
  • if any objects in ArrayOne need to be replaced, item with the lowest value for key "item_image_timeStamp" should be replaced first.(oldest object will be replaced first..followed by the the second oldest).

I hope I did not make my question too confusing. Thank you in advance.

2
What have you tried? Also your algorithm is not entirely clear - is the final array meant to contain (a) the original top 5 from First, (b) all of Second, and (c) the youngest of any remains items in First that will fit? Or is the algorithm performed per entry in Second, i.e. adding the 2nd from Second might eject the just added 1st from Second? - CRD
Yes..exactly what you said..the final array is meant to contain (a) the original top 5 from First, (b) all of Second, and (c) the youngest of any remains items in First that will fit - Abbin Varghese

2 Answers

0
votes

Adding, removing and measuring length are all methods on mutable arrays, straight-forward to apply. The interesting part of the question is the sort criteria, which is very well addressed by NSSortDescriptor.

NSArray allows the application of these in groups with sortedArrayUsingDescriptors (notice the plural). So a good approach is to add arrays, sort according to your criteria and truncate to a 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 is considered preferable style
[bigArray addObjectsFromArray:ArrayTwo];

[bigArray sortUsingDescriptors:@[voteDescriptor, dateDescriptor]];
// note: order matters. you want votes sorted first, ties broken with date

The result is the bigArray, now sorted, truncated to the max size.

NSArray *result = [bigArray subarrayWithRange:NSMakeRange(0, MIN(10, bigArray.count))];
0
votes

Yes..exactly what you said..the final array is meant to contain (a) the original top 5 from First, (b) all of Second, and (c) the youngest of any remains items in First that will fit

Just work through your requirement in steps:

  1. If the number of items in First & Second combined is <= 10 just join.

  2. "the original top 5 from First" - Sort First by vote count, you can use a function, block or descriptor based sort. Copy the first 5 (there must be at least 5 due to [1] so no checking required here) items to be your result.

  3. "all of Second" - Append the elements of Second to your result. You now have 6-8 items in your result.

  4. Copy items 6 onwards of your sorted First from [2] and sort the resulting array by age. Append the first 2-4 items, as needed, to your result.

All the above can be done with standard NSArray methods for sorting, copying and appending,

HTH