IComparer compare value of enum










0















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









share|improve this question
























  • 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















0















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









share|improve this question
























  • 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













0












0








0








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












1 Answer
1






active

oldest

votes


















1














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);





share|improve this answer

























  • 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










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
);



);













draft saved

draft discarded


















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









1














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);





share|improve this answer

























  • 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















1














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);





share|improve this answer

























  • 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













1












1








1







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);





share|improve this answer















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);






share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)