Zohaib
Zohaib

Reputation: 19

How to sort cards in by in array acc. to the color type

I just want to sort the cards in payers hand by colours in order Spade, Heart ,Club, Diamnod....right

I have count them and know want to sort....

Note: color sort had already done in upper method i want to sort now in no.s with in same colors...

    public static void noSort(Card temp[]){
    int S=0,H=0,C=0,D=0;
    for(Card x: temp){
        if(x.cardType=="Spade")
            S++;
        else if(x.cardType=="Heart")
            H++;
        else if(x.cardType=="Club")
            C++;
        else
            D++;            
    }
    System.out.println("S: " + S + " H: " + H + " C: " + C + " D: " + D);

    int loc=0;
    Card swap=temp[loc];
    for(loc=0;loc<S;loc++){
        for(int i=0;i<S;i++){
            if(temp[i].cardType=="Spade"){
                if(temp[i].cardValue>temp[i+1].cardValue);{
                    swap=temp[i];
                    temp[i]=temp[i+1];
                    temp[i+1]=swap; 
                }
            }   
        }
    }
}

Upvotes: 0

Views: 112

Answers (2)

Ashot Karakhanyan
Ashot Karakhanyan

Reputation: 2830

So implement Comparable interface and override compareTo method for your Card. And call Collections.sort method for sorting.

Upvotes: 1

Girish
Girish

Reputation: 1717

My friend for color sorting you can try Dutch national flag problem http://en.wikipedia.org/wiki/Dutch_national_flag_problem,

and for number sorting within in the same color you can use selection sort or quick sort

Upvotes: 0

Related Questions