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