user8515431
user8515431

Reputation: 41

Sorting an Array of string which contains integers and characters

Upvotes: 0

Views: 308

Answers (3)

Barmak Shemirani
Barmak Shemirani

Reputation: 31599

You have to have vector or array. If you are not allowed to use vector then create an array by first reading the array and counting the number of items (count). Create an array based on that size, then read the file again and set the array items:

string str;
int count = 0;
while(getline(file, str, ','))
    count++;

string *sarray = new string[count];

file.clear();
file.seekg(0, ios::beg);
int i = 0;
while(getline(file, str, ','))
{
    //trim spaces if any
    str.erase(0, str.find_first_not_of(' '));
    str.erase(str.find_last_not_of(' ') + 1);

    sarray[i] = str;
    i++;
    if(i == count)
        break;
}

To sort the values, break the string in to two parts. For example break H4 in to H and 4. Then run a comparison. The following uses lambda sort. Maybe you want to change it to something similar to the other answer.

std::sort(sarray, sarray + count, [](string a, string b)
{
    //save the first character
    char chA = a[0];
    char chB = b[0];

    //remove the first character
    a.erase(0, 1);
    b.erase(0, 1);

    if(a == "J") a = "11";
    if(a == "Q") a = "12";
    if(a == "K") a = "13";
    if(a == "A") a = "14";

    if(b == "J") b = "11";
    if(b == "Q") b = "12";
    if(b == "K") b = "13";
    if(b == "A") b = "14";

    //convert to integer
    int v1 = stoi(a);
    int v2 = stoi(b);

    //if the two cards have the same values...
    if(v2 == v1) return chA > chB;

    return v1 > v2;
});

For cleanup:

delete[]sarray;

Note, you may wish to add exception handling for stoi

Upvotes: 0

Sandro
Sandro

Reputation: 2786

here is the small example how you sort your cards (you have also to think about error handling it, didn't implement it):

#include <iostream>
#include <algorithm>

using namespace std;

int getCardRange(const std::string& card)
{
    switch(card[1])
    {
        case '2': return 2;
        case '3': return 3;
        case '4': return 4;
        case '5': return 5;
        case '6': return 6;
        case '7': return 7;
        case '8': return 8;
        case '9': return 9;
        case '1': return 10;
        case 'J': return 11;
        case 'Q': return 12;
        case 'K': return 13;
        case 'A': return 14;
    }
    return -1; // error
}

bool compare(const std::string& a, const std::string& b)
{
    return getCardRange(a) > getCardRange(b);
}

int main()
{
    std::string myArray[13] = {"H4","C8","HJ","C9","D10","D5","DK","D2","S7","DJ","SK","H3","H6"};
    // sort using a lambda expression 
    std::sort(std::begin(myArray), std::end(myArray), compare);

    for(auto card : myArray)
    {
        std::cout << card << ' ';
    }
    std::cout << '\n';

    return 0;
}

Upvotes: 1

bpinaya
bpinaya

Reputation: 609

You can use std::sort with vectors (you can read more in the documentation)

It takes as input an array like this:

std::sort(cards.begin(), cards.end());

Upvotes: 0

Related Questions