|Aug 1, 2012, 04:12 PM||#1|
Comparing Two Arrays
I'm trying to figure out how to tell if two NSArrays are equal to each other. I looked at apple's documentation for the instance method isEqualToArray: and it had this to say:
For example, I have one array with the numbers 5, 1, 3 (in that order). Another array has the numbers 3, 1, 5. I would consider these arrays equal for the purposes in my app because they both are the same, except for order. However, isEqualToArray: does take order into account, so I can't use it.
Could someone suggest a way to tell if my two arrays are the same disregarding the order of the objects? Thanks in advance!
|Aug 1, 2012, 06:33 PM||#2|
Are you new to programming?
I do not use obj-C, but regardless of language here are two options that will work:
1) Keep your arrays sorted or sort them before comparison. If you have positive integers and the range is not too large you could maybe use a radix, counting, or bucket sort. Otherwise you would have to use a comparison based sort like quick, merge, or heap sort. You can find more info about sorting here, http://en.wikipedia.org/wiki/Sorting_algorithm
2) Write your own comparison function. If you believe you do not know how, think about the problem for a while. Post a solution that you believe will work, even if it is slow, in pseudo code and ask for feedback on how to improve it.
Last edited by theyoda3; Aug 1, 2012 at 06:40 PM.
|Aug 10, 2012, 12:04 PM||#3|
Look here ..
You will still need to parse through each element.
But I think copying the arrays to a tempArray and Sorting them seems like the best option.
Here's a wacky solution.. keep a NSString as part of your object every time you add a number to the array add it to the string sort it and then string compare it .. thus keeping your orginal array in order..
I said it was wacky didn't I.. shrug..
|Aug 10, 2012, 01:27 PM||#5|
Use NSSet instead of NSArray if order doesn't matter. [NSSet isEqualToSet:NSSet] does exactly what you're describing. You could go through the bother of making a category of NSArray to add this functionality, but it's not worth it.
EDIT: Didn't see the post above me but it doesn't look like you would need any special kind of NSSet to do what the OP wants to do.
|Aug 10, 2012, 03:56 PM||#6|
Converting to sets is pretty fast, and should happen in nearly linear time.
If the arrays could contain multiple copies of the same object, and you need to make sure that the counts between arrays match, you should use NSCountedSet instead of NSIndexSet. (I'm not positive if the NSCountedSet version of isEqualToSet takes object counts into account. If it doesn't, you could add that check fairly easily by first doing an isEqualToSet test, and if that returns true, enumerating one NSCountedSet and comparing the count of each object to the count of the same object in the other set. If all counts match, you know both arrays contain the same number of instances of the exact same objects.)
Duncan Champney, WareTo.
Check out our latest iOS app, Face Dancer, available for free on the App Store.
|Thread Tools||Search this Thread|
|thread||Thread Starter||Forum||Replies||Last Post|
|arrays? subview or submenu?||tacotester1||iPhone/iPad Programming||6||Mar 14, 2014 08:06 PM|
|Please Help with arrays||Bigred101||Mac Programming||7||Mar 13, 2014 07:39 PM|
|hard drive arrays||rdking647||Buying Tips and Advice||2||Dec 6, 2013 08:45 PM|
|Storing bundle files in arrays||xArtx||iPhone/iPad Programming||13||May 23, 2013 04:13 AM|
|arrays of structs||farmerdoug||Mac Programming||3||Feb 7, 2013 01:47 PM|
All times are GMT -5. The time now is 04:16 AM.