472,122 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

Finding most approximated item in collection object?

Is there any way how to get the item which has the most similar key in
key-value collection object such as Dictionary<or SortedDictionary<>
although there is no matching key? If there is none, is there any
substitution class for enabling this?

Please reply. Thanks in advance.

Hyun-jik Bae
Oct 24 '06 #1
6 1652
to my understanding you are asking about a way to match the keys as such

key = "test"
if test is there then you should get the value of it and else you should get
the value of a key that has "testdata" I am I correct..

If yes then this is not possible and key has to match exactly. If you need
some thing like this then you may have to write a collection that does this
for you by probably exrednign the existing one.

Nirosh.

"Hyun-jik Bae" <im***********@paran.comwrote in message
news:OU**************@TK2MSFTNGP02.phx.gbl...
Is there any way how to get the item which has the most similar key in
key-value collection object such as Dictionary<or SortedDictionary<>
although there is no matching key? If there is none, is there any
substitution class for enabling this?

Please reply. Thanks in advance.

Hyun-jik Bae

Oct 24 '06 #2
KH
You're talking about some fuzzy logic there - first and most importantly you
need to define exactly what "most similar" means, between whatever objects
you're comparing. From there you would probably implement IComparer for the
type and a custom collection that would allow this fuzzy lookup.

For example for String, you could define "most similar" as the first match
less than or equal to a given value, then using a sorted set of values
(untested code here)...

string find = "d";
string[] values = { "a", "c", "e" };
string match = null;

Array.Sort(values);

foreach(string s in values)
{
if (s.CompareTo(find) <= 0)
{
match = s;
break;
}
}


"Hyun-jik Bae" wrote:
Is there any way how to get the item which has the most similar key in
key-value collection object such as Dictionary<or SortedDictionary<>
although there is no matching key? If there is none, is there any
substitution class for enabling this?

Please reply. Thanks in advance.

Hyun-jik Bae
Oct 24 '06 #3
Hi Hyun-jik,

You can subclass Dictionary or SortedDictionary and add your own property to
iterate the Keys collection and return an appropriate value, however you won't
be able to modify the indexer to match similar values. If the hash codes
aren't consistent with the equality comparison then you won't be able to
locate existing keys in the Dictionary.

--
Dave Sexton

"Hyun-jik Bae" <im***********@paran.comwrote in message
news:OU**************@TK2MSFTNGP02.phx.gbl...
Is there any way how to get the item which has the most similar key in
key-value collection object such as Dictionary<or SortedDictionary<>
although there is no matching key? If there is none, is there any
substitution class for enabling this?

Please reply. Thanks in advance.

Hyun-jik Bae

Oct 24 '06 #4
Hyun-jik Bae wrote:
Is there any way how to get the item which has the most similar key in
key-value collection object such as Dictionary<or SortedDictionary<>
although there is no matching key? If there is none, is there any
substitution class for enabling this?
Take a look at the Dictionary (IEqualityComparer<T>) constructor which
allows you to specify custom Equals and GetHashCode methods.

This may not be what you want, though. I get the impression (though I
may be wrong) that you only want to match, say, "Schmidt" if "Smith"
isn't found, while using an IEqualityComparer will always equate
"Schmidt" with "Smith."

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn Great reviews & good sales.
Oct 24 '06 #5
"Hyun-jik Bae" <im***********@paran.comwrote in message
news:OU**************@TK2MSFTNGP02.phx.gbl...
Is there any way how to get the item which has the most similar key in
key-value collection object such as Dictionary<or SortedDictionary<>
although there is no matching key? If there is none, is there any
substitution class for enabling this?
I don't see anyway except to iterate through the entire collection and apply
a comparison yourself. You'd have to rank items by similarity, sort them and
take the highest (or lowest) value.
>
Please reply. Thanks in advance.

Hyun-jik Bae

Oct 24 '06 #6
Hi,

What is the problem that you have? maybe we can help you get a better
solution than the one you are expecting to use

"Hyun-jik Bae" <im***********@paran.comwrote in message
news:OU**************@TK2MSFTNGP02.phx.gbl...
Is there any way how to get the item which has the most similar key in
key-value collection object such as Dictionary<or SortedDictionary<>
although there is no matching key? If there is none, is there any
substitution class for enabling this?

Please reply. Thanks in advance.

Hyun-jik Bae

Oct 24 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Marc L'Ecuyer | last post: by
2 posts views Thread by John Spiegel | last post: by
23 posts views Thread by Darryl Kerkeslager | last post: by
9 posts views Thread by Gerald Lightsey | last post: by
2 posts views Thread by Ash Jones | last post: by
5 posts views Thread by GaryB | last post: by
7 posts views Thread by Brad Baker | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.