var distances = new Dictionary<char, float>();
var nodes = new List<char>();
I have this line to find the smallest distance
nodes.Sort((x, y) => distances[x] - distances[y]);
When I use int
it works well, but when I used float
I got a message
cannot convert lambda expression to type 'System.Collections.Generic.IComparer' because it is not a delegate type
Do you have an idea?
You can't convert your lambda expression into a Comparison<char>
(which is what you want) because it returns a float
- you've effectively got a Func<char, char, float>
there, whereas Comparison<char>
is closer to Func<char, char, int>
.
The simplest approach is to use float.CompareTo
:
nodes.Sort((x, y) => distances[x].CompareTo(distances[y]));
Or if you don't need to sort in-place, you could use LINQ:
var sorted = nodes.OrderBy(x => distances[x]);
See more on this question at Stackoverflow