java 按花色对一副牌进行排序,然后排名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4650042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:22:15  来源:igfitidea点击:

Sorting a deck of cards by suit and then rank

javaalgorithmsorting

提问by Igor

I have this Java class:

我有这个 Java 类:

class Card
{
    private Suit suit;
    private int  rank;
}

(Suitis an enum)

Suit是一个枚举)

There are four suits with ranks 1-9 each and four suits with a single possible rank 0. Each card exists in an unknown but constant among all cards number of copies. How would I sort a deck in a set order of suits, and by increasing rank in each suit?

有四种花色,每个花色为 1-9,四种花色具有单个可能的 0 阶。每张牌都以未知但恒定的形式存在于所有牌的副本数中。我将如何按照一套套装顺序对一副套牌进行排序,并通过增加每个套装的排名?

采纳答案by dom farr

Have a look at implementing Comparable on the enum.

看看在枚举上实现 Comparable 。

回答by Rup

You'll need to either

你需要要么

  1. implement the Comparableinterface on the card object: add a compareTofunction that determines whether another card should be before or after this one in the sort order
  2. implement a Comparatorobject that accepts two cards and indicates which order they should appear in
  1. 在卡片对象上实现Comparable接口:添加一个compareTo函数来确定在排序顺序中另一张卡片应该在这张卡片之前还是之后
  2. 实现一个Comparator对象,它接受两张卡片并指示它们应该出现的顺序

and then you can use Collections.sorton your list.

然后你可以在你的列表中使用Collections.sort

回答by Amir Afghani

Make Rank an enum too and you can deal a sorted deck as such:

也让 Rank 成为一个枚举,你可以像这样处理一个排序的甲板:

    for (Suit suit : Suit.values())
        for (Rank rank : Rank.values())
            deck.add(new Card(rank, suit));

回答by jon_darkstar

make it implement the Comparableinterface. Theres only only one method you'll need to write. Then they can be compared to each other, and you have a number of existing sort options, such as static Arrays.sortif you have an array or Collections.sortif its any kind of Collection(List, Vector, etc)

让它实现Comparable接口。您只需要编写一种方法。然后可以将它们相互比较,并且您有许多现有的排序选项,例如静态,Arrays.sort如果您有数组或Collections.sort任何类型的Collection(列表、向量等)

Besides implementing on Cardyou might have to do the same for Suitdepending on how its done.

除了实施之外,Card您可能还必须Suit根据其完成方​​式执行相同的操作。

回答by GeekyDaddy

Here would be an example of the Card Class. As the questions states the Suit would be of a specific class while the Rank would be an integer (in this example I didn't implement rank validation). Implementing the Comparable class allows a Card class to compare another Card Class. So that a List/Set of Cards can be sorted.

下面是 Card 类的一个例子。正如问题所述, Suit 将属于特定类别,而 Rank 将是一个整数(在此示例中,我没有实施排名验证)。实现 Comparable 类允许一个 Card 类比较另一个 Card 类。以便可以对卡片列表/卡片集进行排序。

public class Card implements Comparable<Card>{

    private SUIT cardSuit;
    private int cardRank;

    public enum SUIT {SPADE, CLUB, HEART, DIAMOND};

    public Card(int cardRank, SUIT cardSuit) {
        this.cardSuit = cardSuit;
        this.cardRank = cardRank;
    }

    /**
     * Generates a random card
     */
    public Card(){
        this((int) (Math.random() * 9) , SUIT.values()[(int) (Math.random() * SUIT.values().length)]);
    }

    public String getSuit() {
        return cardSuit.toString();
    }

    public int getRank() {
        return cardRank;
    }

    @Override
    public int compareTo(Card2 o) {
        if (this.cardRank == o.cardRank) {
            return this.cardSuit.compareTo(o.cardSuit);
        } else {
            return o.cardRank - this.cardRank;
        }
    }

}

回答by Edward Doolittle

A fast way to accomplish this task is by Radix Sort. Set up an array of lists of card values, then walk through your deck, placing each card into the appropriate list as you encounter it. Then merge all the lists together into a partially sorted deck. Now do the same thing, only with an array of lists of suits. Merge all the lists together and you should have a sorted deck.

完成此任务的一种快速方法是使用基数排序。设置一组卡片值列表,然后遍历您的牌组,在遇到每张卡片时将其放入适当的列表中。然后将所有列表合并到一个部分排序的甲板上。现在做同样的事情,只用一组西装列表。将所有列表合并在一起,你应该有一个排序的甲板。