C
C#2y ago
LukeJ

Cannot create comparer [Answered]

I'm trying to make a comparer to pass around but it's refusing to let me define it. Here's the code
Comparer<Card> trumpComparison = new Func<Card, Card, int>((card1, card2) => (card1.suite == trump ? 1 : 0) - (card2.suite == trump ? 1 : 0));
Comparer<Card> trumpComparison = new Func<Card, Card, int>((card1, card2) => (card1.suite == trump ? 1 : 0) - (card2.suite == trump ? 1 : 0));
Card in this case being
public readonly record struct Card(Suite suite, Rank rank);
public readonly record struct Card(Suite suite, Rank rank);
I've tried doing it as a lambda and it complained it wasn't a delegate. So instead I give it a func, and now it's telling me it cannot convert it to a comparer. What am I missing here?
3 Replies
reflectronic
reflectronic2y ago
you need to do Comparer.Create(func)
LukeJ
LukeJ2y ago
Aaaaah, that makes sense. Thanks.
Accord
Accord2y ago
✅ This post has been marked as answered!