how to sort a deck of cards by suit or rank

Can somebody give me some suggestion as to how i can sort a deck of cards ( the vector deck) by suit or rank ? here is my starter code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
 /*

Using command line arguments read in the file called Deck.txt
*/

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <fstream>

using namespace std;

//Enum types for Card objects

enum CardSuit { CLUBS, DIAMONDS, HEARTS, SPADES };

enum CardRank {ACE = 1, TWO, THREE ,FOUR, FIVE ,SIX, SEVEN , EIGHT, NINE, TEN, JACK , QUEEN, KING };

class Card
{
public:
	CardSuit suit;
	CardRank rank;


};





//Create two string arrays to use for overloaded operator << function
string ranks[] = { "", "Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King" };
string suits[] = { "Clubs", "Diamonds", "Hearts", "Spades" };

ostream& operator << (ostream & os, Card const& c) {

	os << ranks[c.rank] << " of " << suits[c.suit]; 
	return os; 


}

void print(vector<Card> const& v)
{

	for (size_t i = 0; i < v.size(); ++i)
	{
		cout << (i + 1) << "." << v[i] << endl; 

	}

	cout << endl; 


}



istream& operator >> (istream& is, Card& c)
{
	if (!is)
		return is;

	string rank, garbage, suit;
	is >> rank >> garbage >> suit;

	for (unsigned i = 1; i < (sizeof(ranks) / sizeof(ranks[1])); ++i)
	{
		if (rank == ranks[i])
			c.rank = CardRank(i);

		    //cout << c.rank << endl; 
	}

	for (unsigned i = 0; i < (sizeof(suits) / sizeof(suits[0])); ++i)
	{
		if (suit == suits[i])
			c.suit = CardSuit(i);
	}

	return is;
}

int main(int argc, char* argv[])
{
	ifstream infile(argv[1]);
	//Test for success
	if (!infile)
	{
		cerr << "Input file opening failed.\n";
		return	EXIT_FAILURE;
	}

	//Load a vector with the Card data 	
	vector<Card> deck;
	Card c;
	while (infile >> c)
	{
		deck.push_back(c);

	}

	print(deck); 

	

	
} 


here is the deck of cards. u can save it as txt file and run it through argv[1]:


Seven of Spades
Three of Clubs
Eight of Spades
Two of Spades
Two of Diamonds
Four of Clubs
Three of Diamonds
Six of Clubs
Nine of Diamonds
Four of Hearts
Five of Clubs
Three of Hearts
Ace of Diamonds
Six of Diamonds
Ace of Spades
Nine of Spades
Queen of Diamonds
Queen of Clubs
Five of Diamonds
Four of Diamonds
Seven of Hearts
Eight of Hearts
Queen of Hearts
Queen of Spades
Ten of Diamonds
Five of Hearts
Six of Hearts
Ten of Spades
Ten of Clubs
Five of Spades
Two of Hearts
Eight of Clubs
Four of Spades
Jack of Hearts
King of Hearts
Jack of Diamonds
Nine of Hearts
Eight of Diamonds
Jack of Clubs
Six of Spades
Nine of Clubs
Jack of Spades
Three of Spades
King of Diamonds
Seven of Diamonds
Ace of Hearts
Two of Clubs
Seven of Clubs
Ten of Hearts
Ace of Clubs
King of Clubs
King of Spades


Last edited on
Define operator less-than:
1
2
3
4
bool operator<(Card const& lhs, Card const& rhs) 
{ return std::tie(lhs.rank, lhs.suit) < std::tie(rhs.rank, rhs.suit); }
... 
std::sort(deck.begin(), deck.end());

http://coliru.stacked-crooked.com/a/d524a7681b9e9d71
> how i can sort a deck of cards ( the vector deck) by suit or rank ?

Provide a custom predicate to std::sort - by suit, by rank, etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <vector>
#include <algorithm>
#include <utility>

void foo( std::vector<Card> deck )
{
    // sort by suit (ascending)
    std::sort( std::begin(deck), std::end(deck),
               [] ( const Card& a, const Card& b ) { return a.suit < b.suit ; } ) ;

    // sort by rank (ascending)
    std::sort( std::begin(deck), std::end(deck),
               [] ( const Card& a, const Card& b ) { return a.rank < b.rank ; } ) ;

    // sort by suit, and within a suit by rank (ascending) 
    std::sort( std::begin(deck), std::end(deck),
               [] ( const Card& a, const Card& b )
               { return std::make_pair(a.suit,a.rank) < std::make_pair(b.suit,b.rank) ; } ) ;

    // sort by rank, and within a rank by suit (ascending)
    std::sort( std::begin(deck), std::end(deck),
               [] ( const Card& a, const Card& b )
               { return std::make_pair(a.rank,a.suit) < std::make_pair(b.rank,b.suit) ; } ) ;
               
    // etc.           
}
Topic archived. No new replies allowed.