Compare 2 arrays ?

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
 
M

Morten Wennevik

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
 
J

Jim Andersen

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
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top