Sorting a 2D array

Hi, I'm working on a problem that asks to create a set of cards in a 2D array, shuffle it then sort it.
I have done everything except the sorting part, because I don't know how to make the string "Hearts-Ace" greater than "Hearts-King", and how to make Hearts > Diamonds > Spades > Clubs.
Can anyone help ??
You could try an std::map<string,int>. I can't really give you any more help without more information.
If you have numerical values for your cards, you could implement a sorting algorithm
http://en.wikipedia.org/wiki/Sorting_algorithm
no, the array of cards is of string type
Seeing as you're using strings, your could create an itorator and then typecast (int)string[ itor ] and use that for comparison. when you typecast a string, it will give you the ascii, decimal equivalent.
http://bestofthisweb.com/blogs/wp-content/uploads/2009/11/ascii_table2.jpg

A lowercase 'e' = 101


This code:
1
2
3
4
	std::string input = "Lynx";

	std::cout << input[ 3 ] << '\n';
	std::cout << (int)input[ 3 ] << '\n';


Produces this output:
x
120
Last edited on
You have no class for a card? So you have:

 
string Deck[4][13]


with each card looking like
1
2
3
4
Deck[0][0] = "Spade-Ace"
Deck[0][1] = "Spade-Two"
/etc
Deck[3][12] = "Heart-King"


If so, you should consider implementing your own card class, it will make this task, and every other task you could possibly do with a card easier.

To do it with what you have (If you really must, for whatever ungodly reason), I would use a regular expression to split each card from the -, use standard string comparison to sort first by suit into Deck[x], then using an insertion sort on the value of the card using a function to determine the value in an integer, such as:
1
2
3
4
5
int foo(string bar)
{
  if(bar == "Ace") return 1;
  //etc
}



But you should really, really make your own card class and make everything 10x easier
Last edited on
Topic archived. No new replies allowed.