Compare 2 arrays ?

  • Thread starter Thread starter Jim Andersen
  • Start date Start date
J

Jim Andersen

Is there anything built into .NET that is good (or rather easy) at comparing
?

I have some data (in an array). I make a copy of this array, and the user
changes some of the data, or maybe he doesn't. Then he clicks a button and
my code runs.

So now I want to see if the user made some changes to the data in the array,
or if the user just looked at the data.

I could write some code that loops through the arrays, but is there some
easier solution ?
Like populating 2 datatables. Can they be compared ? Or convert them to XML
docs... anything that can compare 2 xml docs ?

/jim
 
Hi Jim,

One loop ought to be enough, which is easier than populating DataTables
anyway, or compare XML. Or you could create a comparing class and use
that when comparing, but in the end the loop might be best.

for(int i = 0; i < list.Length; i++)
if( ((class)list).CompareTo(list2) != 0 )
// change
 
David said:
What are these arrays of (what type)?

I actually start out by having the data in 2 datatables. One column is
system.string the other is system.byte

/jim
 
Doing this type of comparison is always a little awkward. Effectively,
you're trying to see if, given 2 sets, whether the intersection of the two
sets is identical to both sets individually (I'm thinking in terms of Venn
Diagrams here). I don't know if the elements of each set have to be in the
same order or not. Generally, when dealing with sets as an abstract concept,
its not an issue.

Given that: I've read 3 or 4 different ways that this cam be acomplished.
None of then are really as efficient as I think we'd all like for them to be
because you have to do a member by member comparison of each element in the
sets..here's the one I use in .NET..

Lets say I have 2 Sets A and B (Collections, Arrays, or some other class
that implements an aggregation)

1) if the sets have different numbers of items, the sets are not equal.
2) create an Int32 array (arrA) the same size as Set A
3) Populate the Array with the Hash Codes of all of the members of Set A
4) For each member of Set B
a) get the member's HashCode
b) Do a BinarySearch of arrA to see if there's a matching Hash Code
c) if there is not, then the sets are not equal
5) if you got through step 4 without any problems, then the sets must be
equal (you already checked to see if the sets were of the same size)

Granted, I always override GetHashCode on the classes I create, so I know
that the hases will correctly Reflect state equivelance in my classes.
 
Back
Top