|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
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 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. Show quote "Hyun-jik Bae" <imays_NOSPAM_@paran.com> wrote in message news:OUU4G4w9GHA.1200@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 > 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; } } Show quote "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 > > > 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. -- Show quoteDave Sexton "Hyun-jik Bae" <imays_NOSPAM_@paran.com> wrote in message news:OUU4G4w9GHA.1200@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 > Hyun-jik Bae wrote:
> Is there any way how to get the item which has the most similar key in Take a look at the Dictionary (IEqualityComparer<T>) constructor which> 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? 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. "Hyun-jik Bae" <imays_NOSPAM_@paran.com> wrote in message I don't see anyway except to iterate through the entire collection and apply news:OUU4G4w9GHA.1200@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? a comparison yourself. You'd have to rank items by similarity, sort them and take the highest (or lowest) value. Show quote > > Please reply. Thanks in advance. > > Hyun-jik Bae > 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 Show quote "Hyun-jik Bae" <imays_NOSPAM_@paran.com> wrote in message news:OUU4G4w9GHA.1200@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 > |
|||||||||||||||||||||||