- Home /
Comparing two objects properties for the closest
I have an object with some bool properties, I want to find another object with the closest characteristics (even if they're not totally similar).
Is there a way to do that? Putting the properties in an ArrayList / Dictionary and comparing?
Thanks!
You could write your own comparison function, sure. $$anonymous$$aybe give each item a "score" based on the number of values which match the original item?
//for example
if (original.fieldA == candidate.fieldA) { score++; }
Once you have a collection to test and a comparison function to test it with, the algorithm is pretty close to typical "find highest value in list" problems.
Yes, I was thinking something similar. But I want it to be scalable, so when I add more properies, I don't need to add more code. Any ideas?
Ah. Well, in that case, you could either keep your properties in some sort of collection (a dictionary of strings to bools?), or you could use System.Reflection to grab all fields dynamically.
Yes, may be using a Dictionary with a for, should solve this. Thanks! I'll try it.
Your answer
Follow this Question
Related Questions
Default Null Value for Key Not Found in Dictionary? 1 Answer
Hashtable(or generic dictionary) vs Enum - when do I use which? 1 Answer
String as variable name, reflection or dictionary or other 2 Answers
Comparing value of multiple ints and returning the largest. 3 Answers
JS objects in Unityscript || BCE0048: Type 'Object' does not support slicing. 0 Answers