IComparer compare value of enum
I am implementing a compare method (IComparer
) for the game Manille and I need to compare certain cards (enum values) with each other.
I want to order them in a certain way so that Number 10 is the biggest card and Ace the second biggest.
There is also a certain symbol that is always higher then other cards (for example Hearts)
The problem:
I don't know how to tell that Nummer.Tien is bigger then B, D, H
Also Ace has to be the second highest card, but the algorithm is putting him on the first place.
public class ManilleComparer : IComparer<Kaart>
private Kleur symbol; // the symbol that is higher
public ManilleComparer(Kleur symbol)
this.symbol = symbol;
public int Compare(Kaart x, Kaart y)
int compareTo = x.Kleur.CompareTo(y.Kleur); //check symbols against each other
if (compareTo == 0) // if value is 0 they have the same symbol
compareTo = x.Nummer.CompareTo(y.Nummer); // order them
return compareTo;
return x.Nummer.CompareTo(y.Nummer); // else compare values
public enum Nummer Aas, Twee, Drie, Vier, Vijf, Zes, Zeven, Acht, Negen, Tien, Boer, Dame, Heer // Ace, Two, Three, Four, Five, Six, Zeven, Eight, Nine, Ten, 11, 12, 13
public enum Kleur Schoppen, Harten, Klaveren, Ruiten
I have a Sort methot that sorts all the cards in the correct orde, which words fine:
public int CompareTo(object obj)
Kaart dezeKaart = this;
Kaart andereKaart = obj as Kaart;
int compareTo = dezeKaart.Kleur.CompareTo(andereKaart.Kleur);
if(compareTo == 0)
compareTo = dezeKaart.Nummer.CompareTo(andereKaart.Nummer);
return compareTo;
return compareTo;
The outcome of this sort method is:
// ♠A - ♠2 - ♠3 - ♠4 - ♠5 - ♠6 - ♠7 - ♠8 - ♠9 - ♠10 - ♠B - ♠D - ♠H
// ♥A - ♥2 - ♥3 - ♥4 - ♥5 - ♥6 - ♥7 - ♥8 - ♥9 - ♥10 - ♥B - ♥D - ♥H
// ♣A - ♣2 - ♣3 - ♣4 - ♣5 - ♣6 - ♣7 - ♣8 - ♣9 - ♣10 - ♣B - ♣D - ♣H
// ♦A - ♦2 - ♦3 - ♦4 - ♦5 - ♦6 - ♦7 - ♦8 - ♦9 - ♦10 - ♦B - ♦D - ♦H
I now want to implement Icompare
to have this as an outcome:
// ♦7 - ♦8 - ♦9 - ♦B - ♦D - ♦H - ♦A - ♦10
// ♣7 - ♣8 - ♣9 - ♣B - ♣D - ♣H - ♣A - ♣10
// ♠7 - ♠8 - ♠9 - ♠B - ♠D - ♠H - ♠A - ♠10
// ♥7 - ♥8 - ♥9 - ♥B - ♥D - ♥H - ♥A - ♥10
c# enums icomparable icomparer
|
show 5 more comments
I am implementing a compare method (IComparer
) for the game Manille and I need to compare certain cards (enum values) with each other.
I want to order them in a certain way so that Number 10 is the biggest card and Ace the second biggest.
There is also a certain symbol that is always higher then other cards (for example Hearts)
The problem:
I don't know how to tell that Nummer.Tien is bigger then B, D, H
Also Ace has to be the second highest card, but the algorithm is putting him on the first place.
public class ManilleComparer : IComparer<Kaart>
private Kleur symbol; // the symbol that is higher
public ManilleComparer(Kleur symbol)
this.symbol = symbol;
public int Compare(Kaart x, Kaart y)
int compareTo = x.Kleur.CompareTo(y.Kleur); //check symbols against each other
if (compareTo == 0) // if value is 0 they have the same symbol
compareTo = x.Nummer.CompareTo(y.Nummer); // order them
return compareTo;
return x.Nummer.CompareTo(y.Nummer); // else compare values
public enum Nummer Aas, Twee, Drie, Vier, Vijf, Zes, Zeven, Acht, Negen, Tien, Boer, Dame, Heer // Ace, Two, Three, Four, Five, Six, Zeven, Eight, Nine, Ten, 11, 12, 13
public enum Kleur Schoppen, Harten, Klaveren, Ruiten
I have a Sort methot that sorts all the cards in the correct orde, which words fine:
public int CompareTo(object obj)
Kaart dezeKaart = this;
Kaart andereKaart = obj as Kaart;
int compareTo = dezeKaart.Kleur.CompareTo(andereKaart.Kleur);
if(compareTo == 0)
compareTo = dezeKaart.Nummer.CompareTo(andereKaart.Nummer);
return compareTo;
return compareTo;
The outcome of this sort method is:
// ♠A - ♠2 - ♠3 - ♠4 - ♠5 - ♠6 - ♠7 - ♠8 - ♠9 - ♠10 - ♠B - ♠D - ♠H
// ♥A - ♥2 - ♥3 - ♥4 - ♥5 - ♥6 - ♥7 - ♥8 - ♥9 - ♥10 - ♥B - ♥D - ♥H
// ♣A - ♣2 - ♣3 - ♣4 - ♣5 - ♣6 - ♣7 - ♣8 - ♣9 - ♣10 - ♣B - ♣D - ♣H
// ♦A - ♦2 - ♦3 - ♦4 - ♦5 - ♦6 - ♦7 - ♦8 - ♦9 - ♦10 - ♦B - ♦D - ♦H
I now want to implement Icompare
to have this as an outcome:
// ♦7 - ♦8 - ♦9 - ♦B - ♦D - ♦H - ♦A - ♦10
// ♣7 - ♣8 - ♣9 - ♣B - ♣D - ♣H - ♣A - ♣10
// ♠7 - ♠8 - ♠9 - ♠B - ♠D - ♠H - ♠A - ♠10
// ♥7 - ♥8 - ♥9 - ♥B - ♥D - ♥H - ♥A - ♥10
c# enums icomparable icomparer
And the problem is?
– InBetween
Nov 11 '18 at 13:27
Sorry, updated my post
– Mike L
Nov 11 '18 at 13:31
1
Well, give your enumeration members values that enforces the card value hierarchy. You know you can do that, right?enum MyEnum One = 1, Three = 3, //etc.
– InBetween
Nov 11 '18 at 13:33
I know but that will mess up my other Sort method, which the outcome must be: // ♠A - ♠2 - ♠3 - ♠4 - ♠5 - ♠6 - ♠7 - ♠8 - ♠9 - ♠10 - ♠B - ♠D - ♠H // ♥A - ♥2 - ♥3 - ♥4 - ♥5 - ♥6 - ♥7 - ♥8 - ♥9 - ♥10 - ♥B - ♥D - ♥H ... And this sort method must be: // ♦7 - ♦8 - ♦9 - ♦B - ♦D - ♦H - ♦A - ♦10 // ♣7 - ♣8 - ♣9 - ♣B - ♣D - ♣H - ♣A - ♣10 // ♠7 - ♠8 - ♠9 - ♠B - ♠D - ♠H - ♠A - ♠10 // ♥7 - ♥8 - ♥9 - ♥B - ♥D - ♥H - ♥A - ♥10
– Mike L
Nov 11 '18 at 13:34
1
Test your code by creating only two cards (the ones that don't work) and debug your code. See where the logic is wrong and fix it.
– CodingYoshi
Nov 11 '18 at 14:11
|
show 5 more comments
I am implementing a compare method (IComparer
) for the game Manille and I need to compare certain cards (enum values) with each other.
I want to order them in a certain way so that Number 10 is the biggest card and Ace the second biggest.
There is also a certain symbol that is always higher then other cards (for example Hearts)
The problem:
I don't know how to tell that Nummer.Tien is bigger then B, D, H
Also Ace has to be the second highest card, but the algorithm is putting him on the first place.
public class ManilleComparer : IComparer<Kaart>
private Kleur symbol; // the symbol that is higher
public ManilleComparer(Kleur symbol)
this.symbol = symbol;
public int Compare(Kaart x, Kaart y)
int compareTo = x.Kleur.CompareTo(y.Kleur); //check symbols against each other
if (compareTo == 0) // if value is 0 they have the same symbol
compareTo = x.Nummer.CompareTo(y.Nummer); // order them
return compareTo;
return x.Nummer.CompareTo(y.Nummer); // else compare values
public enum Nummer Aas, Twee, Drie, Vier, Vijf, Zes, Zeven, Acht, Negen, Tien, Boer, Dame, Heer // Ace, Two, Three, Four, Five, Six, Zeven, Eight, Nine, Ten, 11, 12, 13
public enum Kleur Schoppen, Harten, Klaveren, Ruiten
I have a Sort methot that sorts all the cards in the correct orde, which words fine:
public int CompareTo(object obj)
Kaart dezeKaart = this;
Kaart andereKaart = obj as Kaart;
int compareTo = dezeKaart.Kleur.CompareTo(andereKaart.Kleur);
if(compareTo == 0)
compareTo = dezeKaart.Nummer.CompareTo(andereKaart.Nummer);
return compareTo;
return compareTo;
The outcome of this sort method is:
// ♠A - ♠2 - ♠3 - ♠4 - ♠5 - ♠6 - ♠7 - ♠8 - ♠9 - ♠10 - ♠B - ♠D - ♠H
// ♥A - ♥2 - ♥3 - ♥4 - ♥5 - ♥6 - ♥7 - ♥8 - ♥9 - ♥10 - ♥B - ♥D - ♥H
// ♣A - ♣2 - ♣3 - ♣4 - ♣5 - ♣6 - ♣7 - ♣8 - ♣9 - ♣10 - ♣B - ♣D - ♣H
// ♦A - ♦2 - ♦3 - ♦4 - ♦5 - ♦6 - ♦7 - ♦8 - ♦9 - ♦10 - ♦B - ♦D - ♦H
I now want to implement Icompare
to have this as an outcome:
// ♦7 - ♦8 - ♦9 - ♦B - ♦D - ♦H - ♦A - ♦10
// ♣7 - ♣8 - ♣9 - ♣B - ♣D - ♣H - ♣A - ♣10
// ♠7 - ♠8 - ♠9 - ♠B - ♠D - ♠H - ♠A - ♠10
// ♥7 - ♥8 - ♥9 - ♥B - ♥D - ♥H - ♥A - ♥10
c# enums icomparable icomparer
I am implementing a compare method (IComparer
) for the game Manille and I need to compare certain cards (enum values) with each other.
I want to order them in a certain way so that Number 10 is the biggest card and Ace the second biggest.
There is also a certain symbol that is always higher then other cards (for example Hearts)
The problem:
I don't know how to tell that Nummer.Tien is bigger then B, D, H
Also Ace has to be the second highest card, but the algorithm is putting him on the first place.
public class ManilleComparer : IComparer<Kaart>
private Kleur symbol; // the symbol that is higher
public ManilleComparer(Kleur symbol)
this.symbol = symbol;
public int Compare(Kaart x, Kaart y)
int compareTo = x.Kleur.CompareTo(y.Kleur); //check symbols against each other
if (compareTo == 0) // if value is 0 they have the same symbol
compareTo = x.Nummer.CompareTo(y.Nummer); // order them
return compareTo;
return x.Nummer.CompareTo(y.Nummer); // else compare values
public enum Nummer Aas, Twee, Drie, Vier, Vijf, Zes, Zeven, Acht, Negen, Tien, Boer, Dame, Heer // Ace, Two, Three, Four, Five, Six, Zeven, Eight, Nine, Ten, 11, 12, 13
public enum Kleur Schoppen, Harten, Klaveren, Ruiten
I have a Sort methot that sorts all the cards in the correct orde, which words fine:
public int CompareTo(object obj)
Kaart dezeKaart = this;
Kaart andereKaart = obj as Kaart;
int compareTo = dezeKaart.Kleur.CompareTo(andereKaart.Kleur);
if(compareTo == 0)
compareTo = dezeKaart.Nummer.CompareTo(andereKaart.Nummer);
return compareTo;
return compareTo;
The outcome of this sort method is:
// ♠A - ♠2 - ♠3 - ♠4 - ♠5 - ♠6 - ♠7 - ♠8 - ♠9 - ♠10 - ♠B - ♠D - ♠H
// ♥A - ♥2 - ♥3 - ♥4 - ♥5 - ♥6 - ♥7 - ♥8 - ♥9 - ♥10 - ♥B - ♥D - ♥H
// ♣A - ♣2 - ♣3 - ♣4 - ♣5 - ♣6 - ♣7 - ♣8 - ♣9 - ♣10 - ♣B - ♣D - ♣H
// ♦A - ♦2 - ♦3 - ♦4 - ♦5 - ♦6 - ♦7 - ♦8 - ♦9 - ♦10 - ♦B - ♦D - ♦H
I now want to implement Icompare
to have this as an outcome:
// ♦7 - ♦8 - ♦9 - ♦B - ♦D - ♦H - ♦A - ♦10
// ♣7 - ♣8 - ♣9 - ♣B - ♣D - ♣H - ♣A - ♣10
// ♠7 - ♠8 - ♠9 - ♠B - ♠D - ♠H - ♠A - ♠10
// ♥7 - ♥8 - ♥9 - ♥B - ♥D - ♥H - ♥A - ♥10
public int CompareTo(object obj)
Kaart dezeKaart = this;
Kaart andereKaart = obj as Kaart;
int compareTo = dezeKaart.Kleur.CompareTo(andereKaart.Kleur);
if(compareTo == 0)
compareTo = dezeKaart.Nummer.CompareTo(andereKaart.Nummer);
return compareTo;
return compareTo;
public int CompareTo(object obj)
Kaart dezeKaart = this;
Kaart andereKaart = obj as Kaart;
int compareTo = dezeKaart.Kleur.CompareTo(andereKaart.Kleur);
if(compareTo == 0)
compareTo = dezeKaart.Nummer.CompareTo(andereKaart.Nummer);
return compareTo;
return compareTo;
c# enums icomparable icomparer
c# enums icomparable icomparer
edited Nov 11 '18 at 13:45
Jonathon Reinhart
90.3k20164231
90.3k20164231
asked Nov 11 '18 at 13:23
Mike LMike L
33
33
And the problem is?
– InBetween
Nov 11 '18 at 13:27
Sorry, updated my post
– Mike L
Nov 11 '18 at 13:31
1
Well, give your enumeration members values that enforces the card value hierarchy. You know you can do that, right?enum MyEnum One = 1, Three = 3, //etc.
– InBetween
Nov 11 '18 at 13:33
I know but that will mess up my other Sort method, which the outcome must be: // ♠A - ♠2 - ♠3 - ♠4 - ♠5 - ♠6 - ♠7 - ♠8 - ♠9 - ♠10 - ♠B - ♠D - ♠H // ♥A - ♥2 - ♥3 - ♥4 - ♥5 - ♥6 - ♥7 - ♥8 - ♥9 - ♥10 - ♥B - ♥D - ♥H ... And this sort method must be: // ♦7 - ♦8 - ♦9 - ♦B - ♦D - ♦H - ♦A - ♦10 // ♣7 - ♣8 - ♣9 - ♣B - ♣D - ♣H - ♣A - ♣10 // ♠7 - ♠8 - ♠9 - ♠B - ♠D - ♠H - ♠A - ♠10 // ♥7 - ♥8 - ♥9 - ♥B - ♥D - ♥H - ♥A - ♥10
– Mike L
Nov 11 '18 at 13:34
1
Test your code by creating only two cards (the ones that don't work) and debug your code. See where the logic is wrong and fix it.
– CodingYoshi
Nov 11 '18 at 14:11
|
show 5 more comments
And the problem is?
– InBetween
Nov 11 '18 at 13:27
Sorry, updated my post
– Mike L
Nov 11 '18 at 13:31
1
Well, give your enumeration members values that enforces the card value hierarchy. You know you can do that, right?enum MyEnum One = 1, Three = 3, //etc.
– InBetween
Nov 11 '18 at 13:33
I know but that will mess up my other Sort method, which the outcome must be: // ♠A - ♠2 - ♠3 - ♠4 - ♠5 - ♠6 - ♠7 - ♠8 - ♠9 - ♠10 - ♠B - ♠D - ♠H // ♥A - ♥2 - ♥3 - ♥4 - ♥5 - ♥6 - ♥7 - ♥8 - ♥9 - ♥10 - ♥B - ♥D - ♥H ... And this sort method must be: // ♦7 - ♦8 - ♦9 - ♦B - ♦D - ♦H - ♦A - ♦10 // ♣7 - ♣8 - ♣9 - ♣B - ♣D - ♣H - ♣A - ♣10 // ♠7 - ♠8 - ♠9 - ♠B - ♠D - ♠H - ♠A - ♠10 // ♥7 - ♥8 - ♥9 - ♥B - ♥D - ♥H - ♥A - ♥10
– Mike L
Nov 11 '18 at 13:34
1
Test your code by creating only two cards (the ones that don't work) and debug your code. See where the logic is wrong and fix it.
– CodingYoshi
Nov 11 '18 at 14:11
And the problem is?
– InBetween
Nov 11 '18 at 13:27
And the problem is?
– InBetween
Nov 11 '18 at 13:27
Sorry, updated my post
– Mike L
Nov 11 '18 at 13:31
Sorry, updated my post
– Mike L
Nov 11 '18 at 13:31
1
1
Well, give your enumeration members values that enforces the card value hierarchy. You know you can do that, right?
enum MyEnum One = 1, Three = 3, //etc.
– InBetween
Nov 11 '18 at 13:33
Well, give your enumeration members values that enforces the card value hierarchy. You know you can do that, right?
enum MyEnum One = 1, Three = 3, //etc.
– InBetween
Nov 11 '18 at 13:33
I know but that will mess up my other Sort method, which the outcome must be: // ♠A - ♠2 - ♠3 - ♠4 - ♠5 - ♠6 - ♠7 - ♠8 - ♠9 - ♠10 - ♠B - ♠D - ♠H // ♥A - ♥2 - ♥3 - ♥4 - ♥5 - ♥6 - ♥7 - ♥8 - ♥9 - ♥10 - ♥B - ♥D - ♥H ... And this sort method must be: // ♦7 - ♦8 - ♦9 - ♦B - ♦D - ♦H - ♦A - ♦10 // ♣7 - ♣8 - ♣9 - ♣B - ♣D - ♣H - ♣A - ♣10 // ♠7 - ♠8 - ♠9 - ♠B - ♠D - ♠H - ♠A - ♠10 // ♥7 - ♥8 - ♥9 - ♥B - ♥D - ♥H - ♥A - ♥10
– Mike L
Nov 11 '18 at 13:34
I know but that will mess up my other Sort method, which the outcome must be: // ♠A - ♠2 - ♠3 - ♠4 - ♠5 - ♠6 - ♠7 - ♠8 - ♠9 - ♠10 - ♠B - ♠D - ♠H // ♥A - ♥2 - ♥3 - ♥4 - ♥5 - ♥6 - ♥7 - ♥8 - ♥9 - ♥10 - ♥B - ♥D - ♥H ... And this sort method must be: // ♦7 - ♦8 - ♦9 - ♦B - ♦D - ♦H - ♦A - ♦10 // ♣7 - ♣8 - ♣9 - ♣B - ♣D - ♣H - ♣A - ♣10 // ♠7 - ♠8 - ♠9 - ♠B - ♠D - ♠H - ♠A - ♠10 // ♥7 - ♥8 - ♥9 - ♥B - ♥D - ♥H - ♥A - ♥10
– Mike L
Nov 11 '18 at 13:34
1
1
Test your code by creating only two cards (the ones that don't work) and debug your code. See where the logic is wrong and fix it.
– CodingYoshi
Nov 11 '18 at 14:11
Test your code by creating only two cards (the ones that don't work) and debug your code. See where the logic is wrong and fix it.
– CodingYoshi
Nov 11 '18 at 14:11
|
show 5 more comments
1 Answer
1
active
oldest
votes
You can do the following:
public class ManilleComparer : IComparer<Kaart>
public ManilleComparer()
public ManilleComparer(List<Kleur> PrefKleurRank)
KleurRank = PrefKleurRank;
public ManilleComparer(Kleur LastColor)
KleurRank.Remove(LastColor);
KleurRank.Add(LastColor);
private List<Kleur> KleurRank = new List<Kleur>() Kleur.Ruiten , Kleur.Klaveren, Kleur.Schoppen, Kleur.Harten ;
private List<Nummer> NummerRank = new List<Nummer>() Nummer.Twee, Nummer.Drie, Nummer.Vier, Nummer.Vier, Nummer.Zes, Nummer.Zeven, Nummer.Acht, Nummer.Negen, Nummer.Boer, Nummer.Dame, Nummer.Heer, Nummer.Aas, Nummer.Tien ;
public int Compare(Kaart x, Kaart y)
int compareTo = KleurRank.IndexOf(x.Kleur).CompareTo(KleurRank.IndexOf(y.Kleur)); //check symbols against each other
if (compareTo == 0) // if value is 0 they have the same symbol
compareTo = NummerRank.IndexOf(x.Nummer).CompareTo(NummerRank.IndexOf(y.Nummer));
return compareTo;
you can also pass the order to the contructor to be more flexible
This is how to use it:
Kaart k1 = new Kaart() Kleur = Kleur.Ruiten, Nummer = Nummer.Drie ;
Kaart k2 = new Kaart() Kleur = Kleur.Harten, Nummer = Nummer.Heer ;
Kaart k3 = new Kaart() Kleur = Kleur.Ruiten, Nummer = Nummer.Aas ;
Kaart.ManilleComparer m = new Kaart.ManilleComparer();
List<Kaart> mylist = new List<Kaart>();
mylist.Add(k1);
mylist.Add(k2);
mylist.Add(k3);
mylist.Sort(m);
private List<Kleur> MyKleurRank = new List<Kleur>() Kleur.Ruiten , Kleur.Klaveren, Kleur.Harten , Kleur.Schoppen;
Kaart.ManilleComparer m2 = new Kaart.ManilleComparer(MyKleurRank);
mylist.Sort(m2);
Hi, Thanks for you reply, I appreciate it! However I'm confused of what NummerRank and KleurRank are?
– Mike L
Nov 11 '18 at 14:47
This is your prefered order which you want to have your cards and colors. Look at the order of them compared to how you want to have them sorted, it is equal. I am using it to get the index of the cards which I compare.
– Aldert
Nov 11 '18 at 14:49
It is returning that KleurRank and NummerRank does not exist in current context
– Mike L
Nov 11 '18 at 14:58
Thanks bro!! it works as you predicted :)
– Mike L
Nov 11 '18 at 15:04
You are welcome! Please mark question as answered
– Aldert
Nov 11 '18 at 15:06
|
show 7 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249161%2ficomparer-compare-value-of-enum%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do the following:
public class ManilleComparer : IComparer<Kaart>
public ManilleComparer()
public ManilleComparer(List<Kleur> PrefKleurRank)
KleurRank = PrefKleurRank;
public ManilleComparer(Kleur LastColor)
KleurRank.Remove(LastColor);
KleurRank.Add(LastColor);
private List<Kleur> KleurRank = new List<Kleur>() Kleur.Ruiten , Kleur.Klaveren, Kleur.Schoppen, Kleur.Harten ;
private List<Nummer> NummerRank = new List<Nummer>() Nummer.Twee, Nummer.Drie, Nummer.Vier, Nummer.Vier, Nummer.Zes, Nummer.Zeven, Nummer.Acht, Nummer.Negen, Nummer.Boer, Nummer.Dame, Nummer.Heer, Nummer.Aas, Nummer.Tien ;
public int Compare(Kaart x, Kaart y)
int compareTo = KleurRank.IndexOf(x.Kleur).CompareTo(KleurRank.IndexOf(y.Kleur)); //check symbols against each other
if (compareTo == 0) // if value is 0 they have the same symbol
compareTo = NummerRank.IndexOf(x.Nummer).CompareTo(NummerRank.IndexOf(y.Nummer));
return compareTo;
you can also pass the order to the contructor to be more flexible
This is how to use it:
Kaart k1 = new Kaart() Kleur = Kleur.Ruiten, Nummer = Nummer.Drie ;
Kaart k2 = new Kaart() Kleur = Kleur.Harten, Nummer = Nummer.Heer ;
Kaart k3 = new Kaart() Kleur = Kleur.Ruiten, Nummer = Nummer.Aas ;
Kaart.ManilleComparer m = new Kaart.ManilleComparer();
List<Kaart> mylist = new List<Kaart>();
mylist.Add(k1);
mylist.Add(k2);
mylist.Add(k3);
mylist.Sort(m);
private List<Kleur> MyKleurRank = new List<Kleur>() Kleur.Ruiten , Kleur.Klaveren, Kleur.Harten , Kleur.Schoppen;
Kaart.ManilleComparer m2 = new Kaart.ManilleComparer(MyKleurRank);
mylist.Sort(m2);
Hi, Thanks for you reply, I appreciate it! However I'm confused of what NummerRank and KleurRank are?
– Mike L
Nov 11 '18 at 14:47
This is your prefered order which you want to have your cards and colors. Look at the order of them compared to how you want to have them sorted, it is equal. I am using it to get the index of the cards which I compare.
– Aldert
Nov 11 '18 at 14:49
It is returning that KleurRank and NummerRank does not exist in current context
– Mike L
Nov 11 '18 at 14:58
Thanks bro!! it works as you predicted :)
– Mike L
Nov 11 '18 at 15:04
You are welcome! Please mark question as answered
– Aldert
Nov 11 '18 at 15:06
|
show 7 more comments
You can do the following:
public class ManilleComparer : IComparer<Kaart>
public ManilleComparer()
public ManilleComparer(List<Kleur> PrefKleurRank)
KleurRank = PrefKleurRank;
public ManilleComparer(Kleur LastColor)
KleurRank.Remove(LastColor);
KleurRank.Add(LastColor);
private List<Kleur> KleurRank = new List<Kleur>() Kleur.Ruiten , Kleur.Klaveren, Kleur.Schoppen, Kleur.Harten ;
private List<Nummer> NummerRank = new List<Nummer>() Nummer.Twee, Nummer.Drie, Nummer.Vier, Nummer.Vier, Nummer.Zes, Nummer.Zeven, Nummer.Acht, Nummer.Negen, Nummer.Boer, Nummer.Dame, Nummer.Heer, Nummer.Aas, Nummer.Tien ;
public int Compare(Kaart x, Kaart y)
int compareTo = KleurRank.IndexOf(x.Kleur).CompareTo(KleurRank.IndexOf(y.Kleur)); //check symbols against each other
if (compareTo == 0) // if value is 0 they have the same symbol
compareTo = NummerRank.IndexOf(x.Nummer).CompareTo(NummerRank.IndexOf(y.Nummer));
return compareTo;
you can also pass the order to the contructor to be more flexible
This is how to use it:
Kaart k1 = new Kaart() Kleur = Kleur.Ruiten, Nummer = Nummer.Drie ;
Kaart k2 = new Kaart() Kleur = Kleur.Harten, Nummer = Nummer.Heer ;
Kaart k3 = new Kaart() Kleur = Kleur.Ruiten, Nummer = Nummer.Aas ;
Kaart.ManilleComparer m = new Kaart.ManilleComparer();
List<Kaart> mylist = new List<Kaart>();
mylist.Add(k1);
mylist.Add(k2);
mylist.Add(k3);
mylist.Sort(m);
private List<Kleur> MyKleurRank = new List<Kleur>() Kleur.Ruiten , Kleur.Klaveren, Kleur.Harten , Kleur.Schoppen;
Kaart.ManilleComparer m2 = new Kaart.ManilleComparer(MyKleurRank);
mylist.Sort(m2);
Hi, Thanks for you reply, I appreciate it! However I'm confused of what NummerRank and KleurRank are?
– Mike L
Nov 11 '18 at 14:47
This is your prefered order which you want to have your cards and colors. Look at the order of them compared to how you want to have them sorted, it is equal. I am using it to get the index of the cards which I compare.
– Aldert
Nov 11 '18 at 14:49
It is returning that KleurRank and NummerRank does not exist in current context
– Mike L
Nov 11 '18 at 14:58
Thanks bro!! it works as you predicted :)
– Mike L
Nov 11 '18 at 15:04
You are welcome! Please mark question as answered
– Aldert
Nov 11 '18 at 15:06
|
show 7 more comments
You can do the following:
public class ManilleComparer : IComparer<Kaart>
public ManilleComparer()
public ManilleComparer(List<Kleur> PrefKleurRank)
KleurRank = PrefKleurRank;
public ManilleComparer(Kleur LastColor)
KleurRank.Remove(LastColor);
KleurRank.Add(LastColor);
private List<Kleur> KleurRank = new List<Kleur>() Kleur.Ruiten , Kleur.Klaveren, Kleur.Schoppen, Kleur.Harten ;
private List<Nummer> NummerRank = new List<Nummer>() Nummer.Twee, Nummer.Drie, Nummer.Vier, Nummer.Vier, Nummer.Zes, Nummer.Zeven, Nummer.Acht, Nummer.Negen, Nummer.Boer, Nummer.Dame, Nummer.Heer, Nummer.Aas, Nummer.Tien ;
public int Compare(Kaart x, Kaart y)
int compareTo = KleurRank.IndexOf(x.Kleur).CompareTo(KleurRank.IndexOf(y.Kleur)); //check symbols against each other
if (compareTo == 0) // if value is 0 they have the same symbol
compareTo = NummerRank.IndexOf(x.Nummer).CompareTo(NummerRank.IndexOf(y.Nummer));
return compareTo;
you can also pass the order to the contructor to be more flexible
This is how to use it:
Kaart k1 = new Kaart() Kleur = Kleur.Ruiten, Nummer = Nummer.Drie ;
Kaart k2 = new Kaart() Kleur = Kleur.Harten, Nummer = Nummer.Heer ;
Kaart k3 = new Kaart() Kleur = Kleur.Ruiten, Nummer = Nummer.Aas ;
Kaart.ManilleComparer m = new Kaart.ManilleComparer();
List<Kaart> mylist = new List<Kaart>();
mylist.Add(k1);
mylist.Add(k2);
mylist.Add(k3);
mylist.Sort(m);
private List<Kleur> MyKleurRank = new List<Kleur>() Kleur.Ruiten , Kleur.Klaveren, Kleur.Harten , Kleur.Schoppen;
Kaart.ManilleComparer m2 = new Kaart.ManilleComparer(MyKleurRank);
mylist.Sort(m2);
You can do the following:
public class ManilleComparer : IComparer<Kaart>
public ManilleComparer()
public ManilleComparer(List<Kleur> PrefKleurRank)
KleurRank = PrefKleurRank;
public ManilleComparer(Kleur LastColor)
KleurRank.Remove(LastColor);
KleurRank.Add(LastColor);
private List<Kleur> KleurRank = new List<Kleur>() Kleur.Ruiten , Kleur.Klaveren, Kleur.Schoppen, Kleur.Harten ;
private List<Nummer> NummerRank = new List<Nummer>() Nummer.Twee, Nummer.Drie, Nummer.Vier, Nummer.Vier, Nummer.Zes, Nummer.Zeven, Nummer.Acht, Nummer.Negen, Nummer.Boer, Nummer.Dame, Nummer.Heer, Nummer.Aas, Nummer.Tien ;
public int Compare(Kaart x, Kaart y)
int compareTo = KleurRank.IndexOf(x.Kleur).CompareTo(KleurRank.IndexOf(y.Kleur)); //check symbols against each other
if (compareTo == 0) // if value is 0 they have the same symbol
compareTo = NummerRank.IndexOf(x.Nummer).CompareTo(NummerRank.IndexOf(y.Nummer));
return compareTo;
you can also pass the order to the contructor to be more flexible
This is how to use it:
Kaart k1 = new Kaart() Kleur = Kleur.Ruiten, Nummer = Nummer.Drie ;
Kaart k2 = new Kaart() Kleur = Kleur.Harten, Nummer = Nummer.Heer ;
Kaart k3 = new Kaart() Kleur = Kleur.Ruiten, Nummer = Nummer.Aas ;
Kaart.ManilleComparer m = new Kaart.ManilleComparer();
List<Kaart> mylist = new List<Kaart>();
mylist.Add(k1);
mylist.Add(k2);
mylist.Add(k3);
mylist.Sort(m);
private List<Kleur> MyKleurRank = new List<Kleur>() Kleur.Ruiten , Kleur.Klaveren, Kleur.Harten , Kleur.Schoppen;
Kaart.ManilleComparer m2 = new Kaart.ManilleComparer(MyKleurRank);
mylist.Sort(m2);
edited Nov 11 '18 at 15:29
answered Nov 11 '18 at 14:39
AldertAldert
1,3681212
1,3681212
Hi, Thanks for you reply, I appreciate it! However I'm confused of what NummerRank and KleurRank are?
– Mike L
Nov 11 '18 at 14:47
This is your prefered order which you want to have your cards and colors. Look at the order of them compared to how you want to have them sorted, it is equal. I am using it to get the index of the cards which I compare.
– Aldert
Nov 11 '18 at 14:49
It is returning that KleurRank and NummerRank does not exist in current context
– Mike L
Nov 11 '18 at 14:58
Thanks bro!! it works as you predicted :)
– Mike L
Nov 11 '18 at 15:04
You are welcome! Please mark question as answered
– Aldert
Nov 11 '18 at 15:06
|
show 7 more comments
Hi, Thanks for you reply, I appreciate it! However I'm confused of what NummerRank and KleurRank are?
– Mike L
Nov 11 '18 at 14:47
This is your prefered order which you want to have your cards and colors. Look at the order of them compared to how you want to have them sorted, it is equal. I am using it to get the index of the cards which I compare.
– Aldert
Nov 11 '18 at 14:49
It is returning that KleurRank and NummerRank does not exist in current context
– Mike L
Nov 11 '18 at 14:58
Thanks bro!! it works as you predicted :)
– Mike L
Nov 11 '18 at 15:04
You are welcome! Please mark question as answered
– Aldert
Nov 11 '18 at 15:06
Hi, Thanks for you reply, I appreciate it! However I'm confused of what NummerRank and KleurRank are?
– Mike L
Nov 11 '18 at 14:47
Hi, Thanks for you reply, I appreciate it! However I'm confused of what NummerRank and KleurRank are?
– Mike L
Nov 11 '18 at 14:47
This is your prefered order which you want to have your cards and colors. Look at the order of them compared to how you want to have them sorted, it is equal. I am using it to get the index of the cards which I compare.
– Aldert
Nov 11 '18 at 14:49
This is your prefered order which you want to have your cards and colors. Look at the order of them compared to how you want to have them sorted, it is equal. I am using it to get the index of the cards which I compare.
– Aldert
Nov 11 '18 at 14:49
It is returning that KleurRank and NummerRank does not exist in current context
– Mike L
Nov 11 '18 at 14:58
It is returning that KleurRank and NummerRank does not exist in current context
– Mike L
Nov 11 '18 at 14:58
Thanks bro!! it works as you predicted :)
– Mike L
Nov 11 '18 at 15:04
Thanks bro!! it works as you predicted :)
– Mike L
Nov 11 '18 at 15:04
You are welcome! Please mark question as answered
– Aldert
Nov 11 '18 at 15:06
You are welcome! Please mark question as answered
– Aldert
Nov 11 '18 at 15:06
|
show 7 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249161%2ficomparer-compare-value-of-enum%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
And the problem is?
– InBetween
Nov 11 '18 at 13:27
Sorry, updated my post
– Mike L
Nov 11 '18 at 13:31
1
Well, give your enumeration members values that enforces the card value hierarchy. You know you can do that, right?
enum MyEnum One = 1, Three = 3, //etc.
– InBetween
Nov 11 '18 at 13:33
I know but that will mess up my other Sort method, which the outcome must be: // ♠A - ♠2 - ♠3 - ♠4 - ♠5 - ♠6 - ♠7 - ♠8 - ♠9 - ♠10 - ♠B - ♠D - ♠H // ♥A - ♥2 - ♥3 - ♥4 - ♥5 - ♥6 - ♥7 - ♥8 - ♥9 - ♥10 - ♥B - ♥D - ♥H ... And this sort method must be: // ♦7 - ♦8 - ♦9 - ♦B - ♦D - ♦H - ♦A - ♦10 // ♣7 - ♣8 - ♣9 - ♣B - ♣D - ♣H - ♣A - ♣10 // ♠7 - ♠8 - ♠9 - ♠B - ♠D - ♠H - ♠A - ♠10 // ♥7 - ♥8 - ♥9 - ♥B - ♥D - ♥H - ♥A - ♥10
– Mike L
Nov 11 '18 at 13:34
1
Test your code by creating only two cards (the ones that don't work) and debug your code. See where the logic is wrong and fix it.
– CodingYoshi
Nov 11 '18 at 14:11