0

First, let me show my code.

void findRev(Items* products[], int numOfProd) {

    for (int i = 0; i < (numOfProd - 1); i++) {
        double rev1 = products[i]->price * products[i]->numSold;
        double rev2 = products[i + 1]->price * products[i + 1]->numSold;

        if (rev1 > rev2) {
            Items* temp = products[i];
            Items* temp2 = products[i + 1];
            temp2 = products[i];
            temp = products[i + 1];
        }
    }
}

I have a struct of different items. I'm trying to sort them by revenue, but I have to find the revenue first.

I know I can just add a revenue field to my struct, but my professor said we're not allowed to do that.

I wrote this function but it doesn't work the way i wanted it to. I have used bubble sort and selection sort before. I just cant seem to figure out how to use one of them for this instance. I have looked all through my books and online. And my professor it would be good to find the revenue within the sorting loop. I just can't figure out how. Anyway i can make it work?

Helios093
  • 3
  • 2
  • Do you have to write your own sorting function or can you use a standard algorithm? You might search a little for bubble sort if not. – Retired Ninja Jun 24 '16 at 16:20

2 Answers2

1

The right way to sort is to use std::sort.

The right way to use std::sort when you are sorting via projection is to sort via projection.

template<class F>
struct sort_by_t {
  F f;
  template<class Lhs, class Rhs>
  bool operator()(Lhs const& lhs, Rhs const& rhs) const {
    return f(lhs) < f(rhs);
  }
};
template<class F>
sort_by_t<typename std::decay<F>::type>
sort_by( F&& f ) { return {std::forward<F>(f)}; }

void findRef( Items const*const* products, int numOfProd) {
  std::sort( products, products+numOfProd,
    sortby([](Items const* i) { return i->price * i->numSold; })
  );
}

Your code doesn't even sort, let alone do it reasonably.

The above code efficiently and succinctly sorts the array. The sort_by code is boilerplate, which can be reused elsewhere.

Here is a sort_t-less sort_by in post-C++11:

template<class F>
auto sort_by(F&& f) {
  return [f=std::forward<F>(f)](auto&& lhs, auto&& rhs){
    return f(lhs)<f(rhs);
  };
}
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
0

you can use an array to store revenues first, then sort products depending on that array, like this:

void findRev(Items* products[], int numOfProd) {
    double* rev= new double[numOfProd];
    for (int i = 0; i < numOfProd; i++)
        rev[i] = products[i]->price * products[i]->numSold;

    // bubble sort
    Items *tempItem;
    double tempDouble;
    for(int i=0; i< numOfProd; i++)
      for(int j=0; j< numOfProd - i -1; j++)
         if(rev[j] > rev[j+1]){
           tempDouble = rev[j];
           rev[j] = rev[j+1];
           rev[j+1] = tempDouble;
           tempItem = products[j];
           products[j] = products[j+1];
           products[j+1] = tempItem;
          }

      delete[] rev;
 }
Rahaf Jawish
  • 46
  • 1
  • 4