0

For an assignment I am required to sort data read from a file, using a vector of customers using various parts of their data (all fake of course). This should be sorted by postcode - however, I am having trouble accessing data from postcode.

Problem description:

  • Need to sort a vector of objects by an attribute of said object
  • I am able to access the attribute by itself, but I don't believe I will be able to sort it only having 'postcode'
  • Is there a way to manually select the index of an object as if it was a vector, say index[2] which is where postcode is stored, so I can compare them as a 2D Vector?
  • If not, how would I go about accessing the data in relation to the entire object, within a for loop, so I can manually sort it

my class

class Customer {
public:
    std::string id;
    std::string name;
    int postcode;
    std::string city;
    long double purchases;
    long double returns;
    std::string relatedID; //apparently inaccessible
    Customer(std::string id, std::string name, int postcode, std::string city, double purchases, double returns, std::string relatedID);
    ~Customer();
    Customer(Customer& cs);
    void checkingReport();
    void getPercent();
    void showPostcode();
}

It is reading them into a vector called rsult, which is working as expected - initialising Customer object

vector <Customer*> rsult;

If I am understanding correctly, my postcode should be at index 2 - but I am having a hard time accessing it. What I want to do: (not accuratre)


void sortList() {
    readFile();
    for (int i = 1; i < rsult.size(); i++) {
        if (rsult[i][2] < rsult[i-1][2]
          <ENTER MANUAL SORT ROUTINE HERE>
    }
}

Clearly C++ doesn't care about what I want to do

However, it doesn't seem to want to work the way i envisioned. I have also tried accessing the postcode attribute by making an instance of Customer*, but then I am not sure how to iterate through the list of rsult based on that

This will get me access to postcode, however I'm not sure how to sort it with that information:

for (Customer* c : rsult)
    {
        int posty = c->postcode;
        cout << "Postcode is: " << posty << endl;   
    }

any help would be greatly appreciated

MCLyonzo
  • 3
  • 4
  • Please try to better describe your problem; neither "_I am having trouble_" nor "_I am having a hard time_" nor "_Clearly C++ doesn't care about what I want to do_" are problem descriptions. – underscore_d Apr 09 '21 at 08:55
  • There is nothing indexable in your vector (a pointer to `Customer` is not an array of its member variables). The postcode of an element is `rsult[index]->postcode`. – molbdnilo Apr 09 '21 at 08:59
  • apologies for innacurate descripion, rectified – MCLyonzo Apr 09 '21 at 09:00
  • @molbdnilo that is what I am attempting to do (apologies, very new to c++) - but I'm not sure how I would declare it similar to my last block of code in this question – MCLyonzo Apr 09 '21 at 09:01
  • I suspect that you're assuming that this is harder than it is. You can compare two postcodes: `rsult[i]->postcode < rsult[i-1]->postcode`. Then you swap array elements around, just like if you were sorting integers. – molbdnilo Apr 09 '21 at 09:04
  • I was thinking it was harder than it was. Thanks, you solved my issue – MCLyonzo Apr 09 '21 at 09:26

0 Answers0