You can do:
maxRank = Type.enumIndex(firstRank) > Type.enumIndex(secondRank) ? firstRank : secondRank;
Pregunta
I have a enum type:
enum HandRank {
HighCard;
OnePair;
Set;
TwoPair;
Straight;
Flush;
FullHouse;
Quads;
StraightFlush;
RoyalFlush;
}
And I'd like to compare a pair of values to choose a better one. Something like:
maxRank = <...some method...>(firstRank, secondRank) ? firstRank : secondRank;
Here I suppose that all values of enum follow in prefferable ascending order. What should I do to solve this?
Solución
You can do:
maxRank = Type.enumIndex(firstRank) > Type.enumIndex(secondRank) ? firstRank : secondRank;
Otros consejos
i would write a max function like this:
class HandRanks {
public static function toInt (a:HandRank) return switch a {
case HighCard: 1;
case OnePair: 2;
case Set: 3;
case TwoPair: 4;
case Straight: 5;
case Flush: 6;
case FullHouse: 7;
case Quads: 8;
case StraightFlush: 9;
case RoyalFlush: 10;
}
public static function max (a:HandRank, b:HandRank) {
var a1 = toInt(a);
var b1 = toInt(b);
return if (a1 > b1) a else b;
}
}
// usage
using HandRanks;
HighCard.max(Set); // Set