Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 1, 2012, 04:12 PM   #1
Ides
macrumors member
 
Join Date: Mar 2012
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:
Quote:
Two arrays have equal contents if they each hold the same number of objects and objects at a given index in each array satisfy the isEqual: test.
This is a problem for me, because the arrays I'm using may contain the exact same objects, but in different order. In my app I have many NSArrays that each contain some NSNumber objects. I would like to compare two arrays to see if they each have the exact same numbers occurring the exact same amount of times, regardless of order.

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!
Ides is offline   0 Reply With Quote
Old Aug 1, 2012, 06:33 PM   #2
theyoda3
macrumors member
 
Join Date: Sep 2006
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.
theyoda3 is offline   0 Reply With Quote
Old Aug 10, 2012, 12:04 PM   #3
IDMah
macrumors regular
 
Join Date: May 2011
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..

sort string

Good Luck.
Ian
IDMah is offline   0 Reply With Quote
Old Aug 10, 2012, 12:29 PM   #4
chown33
macrumors 603
 
Join Date: Aug 2009
The problem might also be solved using NSCountedSet, or maybe even NSIndexSet. It depends on exactly how the numbers are being used.
chown33 is offline   0 Reply With Quote
Old Aug 10, 2012, 01:27 PM   #5
EnigMoiD
macrumors member
 
Join Date: Aug 2007
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.
EnigMoiD is offline   1 Reply With Quote
Old Aug 10, 2012, 03:56 PM   #6
Duncan C
macrumors 6502a
 
Duncan C's Avatar
 
Join Date: Jan 2008
Location: Northern Virginia
 
Quote:
Originally Posted by EnigMoiD View Post
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.
Converting the arrays to sets is by far the better way to go. Sorting arrays is slow, and sort time up with the number of elements being sorted by at least n Log(n) for a good sorting algorithm, or n squared for a bad algorithm.

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.)
__________________
Regards,
Duncan Champney, WareTo.
Check out our latest iOS app, Face Dancer, available for free on the App Store.
Duncan C is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
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

Forum Jump

All times are GMT -5. The time now is 04:16 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC