SortedList with duplicate items? 
Author Message
 SortedList with duplicate items?

In general, if I want to have a SortedList that contains duplicates, I
simply write my own IComparer-derived class, and pass it to the constructor
of SortedList. This comparer class does an equality check on the two objects
in question, and if it finds that they're equal, it returns a compare of the
hash codes of the two objects... so in other words, the two "equal" objects
are ordered in pretty much a random fashion.

Now I have occasion to have a SortedList that contains strings, some of
which might be duplicates. My usual trick doesn't work because
String.GetHashCode() is overloaded in such a way that two different string
instances containing the same letters will return the same hash code. Please
give me a way to differentiate between two different string objects that
contain the same string so that I can implement my compare function.

I think what I need is to use is the special numeric index that the CLR
assigns to each object that exists. This is what Object.GetHashCode()
uses... but I can't figure out how to get hold of that numeric index. Any
ideas?

David



Mon, 13 Dec 2004 06:12:13 GMT  
 SortedList with duplicate items?
Can't you simply use Object.ReferenceEquals((object)string1,(object)string2)?

--Bob Grommes

"Rev. Dr. Hulio Manuel Sanchez-Cruz, MCSD, PhD, MD, DDS, CPA, RN, Esq."

Quote:

> In general, if I want to have a SortedList that contains duplicates, I
> simply write my own IComparer-derived class, and pass it to the constructor
> of SortedList. This comparer class does an equality check on the two objects
> in question, and if it finds that they're equal, it returns a compare of the
> hash codes of the two objects... so in other words, the two "equal" objects
> are ordered in pretty much a random fashion.

> Now I have occasion to have a SortedList that contains strings, some of
> which might be duplicates. My usual trick doesn't work because
> String.GetHashCode() is overloaded in such a way that two different string
> instances containing the same letters will return the same hash code. Please
> give me a way to differentiate between two different string objects that
> contain the same string so that I can implement my compare function.

> I think what I need is to use is the special numeric index that the CLR
> assigns to each object that exists. This is what Object.GetHashCode()
> uses... but I can't figure out how to get hold of that numeric index. Any
> ideas?

> David



Mon, 13 Dec 2004 06:38:14 GMT  
 SortedList with duplicate items?
    I noticed that function too, but I'm not sure how I would use it in my
compare function. When I see that two strings are "equal",
Object.ReferenceEquals(string1,string1) would always return false. That
wouldn't help me...what I really need is some way to consistently "order"
string1 and string2. For any other non-string object, I could use
GetHashCode() as a way to consistently order two objects... but what could I
use for a string?

David


Quote:
> Can't you simply use

Object.ReferenceEquals((object)string1,(object)string2)?
Quote:

> --Bob Grommes



Mon, 13 Dec 2004 06:49:33 GMT  
 SortedList with duplicate items?
Strings that are not interned would be typically be stored in different objects
even if their value is the same, so you're right, this is no help to you in
ordering them.

I'm afraid I can't answer the question of how to consistently order them, it's
an area I haven't looked into.

--Bob

"Rev. Dr. Hulio Manuel Sanchez-Cruz, MCSD, PhD, MD, DDS, CPA, RN, Esq."

Quote:

>     I noticed that function too, but I'm not sure how I would use it in my
> compare function. When I see that two strings are "equal",
> Object.ReferenceEquals(string1,string1) would always return false. That
> wouldn't help me...what I really need is some way to consistently "order"
> string1 and string2. For any other non-string object, I could use
> GetHashCode() as a way to consistently order two objects... but what could I
> use for a string?

> David



> > Can't you simply use
> Object.ReferenceEquals((object)string1,(object)string2)?

> > --Bob Grommes



Mon, 13 Dec 2004 23:02:35 GMT  
 SortedList with duplicate items?


Wed, 18 Jun 1902 08:00:00 GMT  
 SortedList with duplicate items?
Did anyone notice David's name?

"Rev. Dr. Hulio Manuel Sanchez-Cruz, MCSD, PhD, MD, DDS, CPA, RN, Esq."

Notice that he's:

- Ordained
- A Phd
- An Medical Doctor
- A Dentist
- An Accountant
- A Registered Nurse
- A Lawyer

He must've spent 20 years of his life in school!

And for all that schooling, he couldn't even spell Julio right =)

-c


Quote:
> Strings that are not interned would be typically be stored in different
objects
> even if their value is the same, so you're right, this is no help to you
in
> ordering them.

> I'm afraid I can't answer the question of how to consistently order them,
it's
> an area I haven't looked into.

> --Bob

> "Rev. Dr. Hulio Manuel Sanchez-Cruz, MCSD, PhD, MD, DDS, CPA, RN, Esq."

> >     I noticed that function too, but I'm not sure how I would use it in
my
> > compare function. When I see that two strings are "equal",
> > Object.ReferenceEquals(string1,string1) would always return false. That
> > wouldn't help me...what I really need is some way to consistently
"order"
> > string1 and string2. For any other non-string object, I could use
> > GetHashCode() as a way to consistently order two objects... but what
could I
> > use for a string?

> > David



> > > Can't you simply use
> > Object.ReferenceEquals((object)string1,(object)string2)?

> > > --Bob Grommes



Mon, 13 Dec 2004 23:12:06 GMT  
 SortedList with duplicate items?


Wed, 18 Jun 1902 08:00:00 GMT  
 SortedList with duplicate items?

Quote:
> He must've spent 20 years of his life in school!

    Ah, not true, my friend. My degrees were purchased online from one of
the finest non-accredited universities on the Internet. No classroom
attendence required.


Mon, 13 Dec 2004 23:29:14 GMT  
 SortedList with duplicate items?

"Rev. Dr. Hulio Manuel Sanchez-Cruz, MCSD, PhD, MD, DDS, CPA, RN, Esq."

Quote:

> > He must've spent 20 years of his life in school!

>     Ah, not true, my friend. My degrees were purchased online from one of
> the finest non-accredited universities on the Internet. No classroom
> attendence required.

Sweet! Which one?

-c



Mon, 13 Dec 2004 23:44:13 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. SortedList, searching for items efficiently

2. Remove duplicated items in an ArrayList

3. Duplicate items in a list

4. Duplicate item in dataset

5. Duplicate menu items

6. duplicated items in ComboBox

7. Duplicate menu items

8. SortedList Collection

9. Odd problem with System.Collections.SortedList

10. deriving from SortedList?

11. SortedList sorted by value.

12. SortedList!!

 

 
Powered by phpBB® Forum Software