Compare SortedList

  • Thread starter Thread starter Frank Esser
  • Start date Start date
F

Frank Esser

Hi,

if there are two objects of type "SortedList", how can I compare them?
I want to find out whether they have exactly the same values and keys in the
same sort order but I do not want to loop through all.

By the way: how can I make a real/deep copy of a sorted list?
 
if there are two objects of type "SortedList", how can I compare them?
I want to find out whether they have exactly the same values and keys in the
same sort order but I do not want to loop through all.

Whether you write the looping code or you find a class that already does it,
the only way to achieve any operation on a whole list is to loop through it.

By the way: how can I make a real/deep copy of a sorted list?

By looping ? :-)

Joanna
 
Frank Esser said:
Hi,

if there are two objects of type "SortedList", how can I compare them?
I want to find out whether they have exactly the same values and keys in
the same sort order but I do not want to loop through all.

I think any meaningfule comparison of lists will involve comparing the
members. There is nothing dictating the binary representation of the list
that would allow you to compare lists as chunks of memory so I would think
you must loop through the members.
By the way: how can I make a real/deep copy of a sorted list?

I lot would depend on the type of data that you were putting in the list
 
A quick and easy thing to do up front would be to compare the Count property
between to collections -- if the Count is not the same they definatly do not
contain the same key/value pairs.

Likewise you can compare two instances with the equality operator to
determine if they are in fact the same object (or you could use
Object.ReferenceEquals() for the same purpose). If the two references point
at the same object then they definatly contain the same key/value pairs.

SortedList a = b = null;

if (a == b)
{
// true
}
else if (a.Count != b.Count)
{
// false
}
else
{
// Loop 'em ...
}
 
Back
Top