Finding most approximated item in collection object?

H

Hyun-jik Bae

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
 
C

Champika Nirosh

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.
 
G

Guest

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;
}
}
 
D

Dave Sexton

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.
 
J

Jon Shemitz

Hyun-jik Bae said:
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."
 
M

Michael C

Hyun-jik Bae said:
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.
 
I

Ignacio Machin \( .NET/ C# MVP \)

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
 

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

Top