views:

111

answers:

3

I had an earlier question about organizing some inputs by name, ID, and then Amount. Now that I have figured out how to get them organized, I need to have 3 outputs; the first by name, the second by ID, and the last by amount. can i use case and switch statements? Every time i tried to, all three outputs were by name.

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 do i get the rest of the data in, so it will have the 2nd output by ID, and the 3rd output by Amount. I think you can add switch statements but when I tired, all three outputs were by the first set, which is by name. any help is appreciated. im not expecting anyone to solve it all for me, just a tip to point me to 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

A: 

Your previous questions accepted answer was to use the std::sort function.

To do what you want here you should have 3 different sort functions

  1. sortByName
  2. sortById
  3. sortByAmount

Then you just call std::sort with the relevant 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
Glen
thanks, but unfortunatley i am not to use the sort function for this assignment.
ahhhh, you should have mentioned that in your question.
Glen
A: 

You should use std::sort for this instead of writing your own sorting code unless you have an exceptionally good reason to write your own.

Writing your own is less clear, less efficient and contains duplicated code in this case.

Edit: Ok, not being allowed to use sort is a good reason.

In that case, my advice is the try to refactor your code into functions and try to make it so that each function does only one thing. That way, the duplication should become apparent and can be eliminated.

The code you posted is trying to do at least four things in one big chunk. Try to separate out your sorting code from what you are sorting. Then you should be able to separate out each of the three ways of sorting.

markh44
Its homework... I'm assuming he's not allowed to use it.
DeadHead
Ah right, I guess that's a good reason!
markh44
+3  A: 

What you're doing right now with your function is mixing the attributes of your data instead of sorting it. You should either use a flag to differ between the column to sort or 3 functions sortByName, sortByID and sortByAmout. And your ifs are wrong. Instead of

if(c[n].CustomerName > c[n+1].CustomerName)
{
   tempname = c[n].CustomerName;
   c[n].CustomerName = c[n+1].CustomerName;
   c[n+1].CustomerName = tempname;
}

it 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;
}
DaClown