sorting a string vector

Is it even possible to sort a string vector?
i have 4 vectors:
player1
player2
player3
player4
each vector has 13 random playing cards in them, in this format:
A H
4 C
3 S
etc..
basically its [value (2-9, T-A, T = ten J Q K A][space][suit (C H S D)]

and i need to sort the cards from highest to lowest in the vector - the suit doesn't matter just as long as A is highest, then K Q J T 9 8 7 6 5 4 3 2. I've googled it and came across http://www.cplusplus.com/forum/general/3605/ but i do not understand anything in it. is there a more simple way of sorting a string vector? and the reason it wasn't an int vector is because then i cant store the letters in it. thanks if anyone knows.
Last edited on
You can define your own function used to compare the elements in the vector ( It should return true if the 1st argument is smaller than the 2nd ) and use the sort standard algorithm: http://www.cplusplus.com/reference/algorithm/sort/
You can define your own function used to compare the elements in the vector ( It should return true if the 1st argument is smaller than the 2nd )

how do i do that?

i used sort and it does the numbers, but the T J Q K A stuff it up, how do i make it arrange them in J Q K A ?

also how would i go about comparing player3.begin()+3 with player2.begin()+1? comparing the 4th card of player3 with the 2nd card of player2?
Last edited on
Here's a possibility:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int cardValue (string& card) {
    switch (card[0]) {
    case '1': return 10;
    case 'J': return 11;
    case 'Q': return 12;
    case 'K': return 13;
    case 'A': return 14;
    default:  return (card[0] - '0');
    }
}

bool cardCompare (string& a, string& b) {
    return cardValue(a) < cardValue(b);
}

sort (cards.begin(), cards.end(), cardCompare); 

I LOVE YOU!
oh my god! that worked!!!!!!!!!!!!!! thanks so much!!!!
Last edited on
Topic archived. No new replies allowed.