methods of sorting vectors of objects

so i am trying to sort 5 cards based on its suit and then its number. the method of determining how to order works i am just having trouble thinking of how to sort them. suggestions will be appreciated.
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

#include <iostream>
#include <vector>
#include <algorithm>
#include <time.h>

struct card{
    char type; 
    int dice, total; 
};

int randomnumber();
char randomletter();
void print(std::vector<card>); 
int convert(char a); 

int main(){
    std::vector<card> cards; cards.resize(5); std::vector<int> copy; 
    srand((unsigned)time(0)); bool run = true; char eord; 
    while(run){
    start: 
        std::cout << "\nWould you like some cards?(Y/N)";
        std::cin >> eord; 
        if(tolower(eord) == 'n') break; 
    //*******************************Numbers********************************************
    //makes dice rolls
    for(int i = 0; i < cards.size(); i++){ cards[i].dice = randomnumber();}
    //makes card types
    for(int i = 0; i < cards.size(); i++){cards[i].type = randomletter();}
        print(cards);
        std::cout << "\nWould you like to sort your cards?(Y/N)";
        std::cin >> eord; 
        if(tolower(eord) == 'n') goto start;//do not really like this  
    //********************************Sort**********************************************
    //sorts dice rolls
        for(int i = 0; i < cards.size();i++){ cards[i].total = cards[i].dice+convert(cards[i].type);}
        for(int i = 0; i < cards.size(); i++){ copy.push_back(cards[i].total);}
              sort(copy.begin(), copy.end()); 
        for(int i = 0; i < cards.size(); i++){
            for(int j = 0; j < cards.size(); j++){
                if(cards[i].total == copy[j]) //what to do here
            }
        }
        print(cards); 
    } 
    //********************************End***********************************************
    return 0;
}

int convert(char a){
    a = tolower(a);
    if(a == 'd') return 30;
    if(a == 's') return 0;
    if(a == 'h') return 15; 
    else return NULL; 
}

void print(std::vector<card> card){
    for(int i = 0; i < 5; i++) std::cout << card[i].dice << " ";
    std::cout << '\n'; 
    for(int i = 0; i < 5; i++) std::cout << card[i].type << " "; 
}

int randomnumber(){
    return rand()%14+2;
}

char randomletter(){
    switch (rand()%3) {
        case 0:
            return 'd';
            break;
        case 1:
            return 's'; 
            break;
        case 2:
            return 'h'; 
            break;
            
        default:
            break;
    }
    return NULL; 
}
Define a less than operator that complies with your sort order and use std::sort to sort the vector.
example/template/outline would be nice.
Last edited on
You'd call sort like:
std::sort(cards.begin(), cards.end(), SortOrder);

You need #include <algorithm>

SortOrder will look something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
bool SortOrder(const card &a, const card &b)
{
    if (a.type < b.type)
        return true;

    if (a.type == b.type)
    {
        if (a.dice < a.dice) // I have no idea what dice is
            return true;
    }

    return false;
}
Last edited on
Ok thank you. That makes so much more sense now. Dice is the card number I was joking with my friend because he gets uptight about those things.
Topic archived. No new replies allowed.