C
C#3y 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
reflectronic3y ago
you need to do Comparer.Create(func)
LukeJ
LukeJOP3y ago
Aaaaah, that makes sense. Thanks.
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server