Comparing/sorting 的 Lambda 表达式以固定的非自然顺序限制值

Lambda Expressions for Comparing/sorting limited values in fixed, non-natural order

我正在尝试使用 Lambda 表达式进行排序,但问题是我无法按正常顺序排序(例如 1、2、3、4)。我需要按另一个顺序排序(例如 2、1、3、4)。 有没有办法使用 Lambda 表达式来做到这一点?
这是我要转换为 Lambda 表达式的代码(如果可能):

private static Comparator<Card> bySuit = new Comparator<Card>() {
    public int compare(Card c1, Card c2) {
        int this_suit = c1.getSuit().value();
        int other_suit = c2.getSuit().value();

        if((this_suit == 2 && other_suit == 1) || (this_suit == 1 && other_suit == 2)){
            return other_suit - this_suit;
        }
        return this_suit - other_suit;
    }
};

编辑
发现我可以使用:

private static Comparator<Card> bySuit = (c1, c2) -> {
        int this_suit = c1.getSuit().value();
        int other_suit = c2.getSuit().value();

        if((this_suit == 2 && other_suit == 1) || (this_suit == 1 && other_suit == 2)){
            return other_suit - this_suit;
        }
        return this_suit - other_suit;
};

但是有没有更好的方法呢?

这与 lambda 表达式无关:如果您想要按固定顺序 sort/compare 的项目数量有限,您可以将该顺序写入数组,然后比较中的值数组:

int[] myOrder = {2, 1, 3, 4};
Comparator<Card> bySuit = (c1, c2) -> Integer.compare(
    myOrder[c1.getSuit().value() - 1], myOrder[c2.getSuit().value() - 1]);

如果您要排序的值不是(几乎)完整的范围,您可以使用 Map<Key, Integer>.