Commute structs with structs and arrays?
I had an earlier question about organizing some of the inputs by name, id, and then sum. Now that I've figured out how to organize them, I need to have 3 outputs; first by name, second by ID and last by quantity. can i use case and switch statements? Every time I tried, all three outputs were named.
this is what I have so far:
void GeneralSort(const int SortItem, const int count, CustomerProfile c[])
{
string tempname;
string tempid;
float tempamount;
for(int iteration = 1; iteration < count; iteration ++)
{
for(int n = 0; n < (count-iteration); n++)
{
if(c[n].CustomerName > c[n+1].CustomerName)
{
tempname = c[n].CustomerName;
c[n].CustomerName = c[n+1].CustomerName;
c[n+1].CustomerName = tempname;
}
if(c[n].CustomerId > c[n+1].CustomerId)
{
tempid = c[n].CustomerId;
c[n].CustomerId = c[n+1].CustomerId;
c[n+1].CustomerId = tempid;
}
if(c[n].AmountDue > c[n+1].AmountDue)
{
tempamount = c[n].AmountDue;
c[n].AmountDue = c[n+1].AmountDue;
c[n+1].AmountDue = tempamount
}
how can i get the rest of the data so it will have a 2nd output by id and a 3rd output of the sum. I think you can add switch statements, but when I got tired, all three outputs were on the first set, which is by name. any help is appreciated. I didn't expect anyone to solve all this for me, just give a hint to point me in the right direction.
Output example:
//by name
name id amount
able b2 24
bob g3 68
carry a4 12
//by id
name id amount
carry a4 12
able b2 24
bob g3 68
//by amount
name id amount
carry a4 12
able b2 24
bob g3 68
What you are doing right now with your function is mixing the attributes of your data, not sorting. You should either use a flag for the difference between the column to be sorted or the 3 functions sortByName, sortByID and sortByAmout. And yours if they are wrong. Instead
if(c[n].CustomerName > c[n+1].CustomerName)
{
tempname = c[n].CustomerName;
c[n].CustomerName = c[n+1].CustomerName;
c[n+1].CustomerName = tempname;
}
he should say something like
CustomerProfile tempItem;
if(c[n].CustomerName > c[n+1].CustomerName)
{
tempItem = c[n];
c[n] = c[n+1];
c[n+1] = tempItem;
}
a source to share
The previous questions answered the use of the std :: sort function.
To do what you want here you must have 3 different sort functions
- sortByName
- sortById
- sortByAmount
Then you just call std :: sort with the appropriate function
std::sort(customers.begin(), customers.end(), &sortByName);
//print your collection here
std::sort(customers.begin(), customers.end(), &sortById);
//print your collection here
std::sort(customers.begin(), customers.end(), &sortByAmount);
//print your collection here
a source to share
You should use std :: sort for this instead of writing your own sorting code, unless you have a very good reason to write your own.
Writing your own code is less clear, less efficient, and contains duplicate code in this case.
Edit: Ok, but sorting is not allowed - good reason.
In this case, my advice is to try to refactor your code into functions and try to make it so that each function only does one thing. Thus, duplication should become apparent and can be eliminated.
The code you posted tries to do at least four things in one big chunk. Try to separate your sorting code from what you are sorting. Then you will be able to separate each of the three sorting methods.
a source to share