思路:给花色赋权重0 13 26 39,排序时某个卡牌的绝对值等于花色+数字,依据这个排序,一次扫描OK。

代码

public class PokerSorter {
// inner class
static class Card {
    public int color;
    public int number;
    @Override
    public String toString(){
        String colorString = "";
        String numberString = "";
        switch(color){
            case 0 : colorString = "♠"; break;
            case 13 : colorString = "♥"; break;
            case 26 : colorString = "♣"; break;
            case 39  : colorString = "♦"; break;
        }
        switch(number){
            case 11 : numberString = "J"; break;
            case 12 : numberString = "Q"; break;
            case 13 : numberString = "K"; break;
            case 14 : numberString = "A"; break;
            default : numberString = String.valueOf(number);break;
        }
        return colorString + numberString;
    }
}

public static void main(String[] args) {    
    // cards init
    List<Card> cards = new ArrayList<Card>(52);
    for (int i = 0; i < 52; i++) {
        Card card = new Card();
        card.number = i % 13 + 2;
        card.color = i / 13 * 13;
        cards.add(card);
    }

    // cards shuffle
    Collections.shuffle(cards);

    // print before sort
    System.out.println("排序前:");
    Iterator<Card> cardsIter = cards.iterator();
    while(cardsIter.hasNext()){
        System.out.print(cardsIter.next());
    }

    // cards sort
    /*
    Collections.sort(cards,new Comparator<Card>(){
        @Override
        public int compare(Card card1, Card card2) {
            int weight1 = card1.color + card1.number;
            int weight2 = card2.color + card2.number;
            return weight1 - weight2;
        }

    });
    */
    //自己写快排
    quickSort(cards, 0, cards.size()-1);
    // print after sort
    System.out.println("");
    System.out.println("排序后:");
    cardsIter = cards.iterator();
    while(cardsIter.hasNext()){
        System.out.print(cardsIter.next());
    }

}

}

输出enter image description here

后续:2gua提示不能用语言库, 将Collection.sort改为自定义quickSort方法,输出不变。

// partition
public static int partition(List<Card> list, int low, int high) {
    // 先以第一个记录为轴
    int tmpColor = list.get(low).color;
    int tmpNumber = list.get(low).number;        
    // 轴的权重
    int weight = list.get(low).color + list.get(low).number;
    while (low < high) {  
        while (low < high && list.get(high).color + list.get(high).number > weight) {  
            high--;  
        }  
        // 比轴小的移到低端
        list.get(low).color = list.get(high).color;
        list.get(low).number = list.get(high).number;
        while (low < high && list.get(low).color + list.get(low).number < weight) {  
            low++;  
        }  
        // 比轴大的移到高端
        list.get(high).color = list.get(low).color;
        list.get(high).number = list.get(low).number;
    }  
    // 轴记录到位
    list.get(low).color = tmpColor;
    list.get(low).number = tmpNumber;
    // 返回轴位置
    return low; 
} 

// quick sort
public static void quickSort(List<Card> list, int low, int high) {  
    if (low < high) {  
        int middle = partition(list, low, high);
        quickSort(list, low, middle - 1);
        quickSort(list, middle + 1, high);
    }  
}