0

I have three NSMutableArray's:

NSMutableArray *dateArray;
NSMutableArray *nameArray;
NSMutableArray *imageArray

I would like to sort the dateArray based on the closest date, and then have the nameArray and imageArray bases on this new sorted order of the dateArray.

How can I accomplish this without changing the structure that I've already setup above?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Dark Knight
  • 297
  • 1
  • 3
  • 17
  • Use a struct with three members (date, name, image) and then create an array of these structs from your input. Sort the struct-array, and then recreate your original arrays. – koen May 19 '17 at 17:35
  • I see - could you please give an example of how I could do this? – Dark Knight May 19 '17 at 17:37
  • Or use a class instead of a struct, I've been reading too much about Swift lately :) – koen May 19 '17 at 17:49
  • See also here: http://stackoverflow.com/questions/7507457/sort-array-of-dictionaries-by-nsdate (they use an array dictionaries to hold the data). – koen May 19 '17 at 18:06
  • Example code for sorting two arrays according to one of them, using a working array of indices or a working array of pointers. [example code](http://stackoverflow.com/questions/44027808/sorting-two-arrays-based-on-one-with-standard-library-copy-steps-avoided/44030600#44030600) . – rcgldr May 19 '17 at 22:23

1 Answers1

1

Without considering why you have three arrays here is an algorithm to "sort" them together:

  • Create a new array the same size as your three arrays filled with integers starting from 0[†].
  • "Sort" this new array using sortedArrayUsingComparator: passing a block to the comparison.
  • Your block will get called with two values from the array, being two integers. Use those integers to index into dataArray and compare the values you find there, returning the result as the block result.

After this you will have an array of integers you can use to indirectly index into your three arrays, e.g. something like:

NSArray *sortedIndicies = [arrayOfInts sortedArrayUsingComparator:...];

// first item according to the sort
id firstDate = dateArray[sortedIndicies[0]];
id firstName = nameArray[sortedIndicies[0]];

You can of course use sortedIndicies to reorder your original 3 arrays if required.

There are other ways to achieve what you require, for example you can "zip" your three arrays into a single array of three items, sort, then "unzip" again. Zip operations are not provided by NSArray so you need to write them (both are a simple loop).

HTH


[†] You can use a "fake" array for this is you wish, for inspiration see this answer - your implementation could be simpler.

Community
  • 1
  • 1
CRD
  • 52,522
  • 5
  • 70
  • 86
  • Hi @CRD, I'm still stuck on this one, could you please help me with the block inside the [arrayOfInts sortedArrayUsingComparator:...]; – Dark Knight Jul 03 '17 at 09:49
  • If you need further help you need to show your code and explain where you are stuck, and to go that you'll need to ask a new question. Then I or someone else will undoubtedly help you. You may add a comment here if you wish with a link to your new question. – CRD Jul 03 '17 at 10:17