简体   繁体   中英

How can I sort in descending order?

I'm trying to sort for the top and second top value of totalRevenue and its name . I've tried to sort them in descending order this way but I could not figure it out how to make it work. Can anyone please help me out? First two entries:

1002 Hammer  23.65  203 
1024 Nails  6.95  400

Here is the code:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

// Structure to hold statistics
struct Selling {
    int productNumber;
    string name;
    double price;
    int soldNumber;
    double totalRevenue[];
};

int main()
{
    ifstream statFile;
    string productName;
    double price;
    int productNumber, soldNumber;
    Selling *productArray[100];
    Selling *aSelling;
    int numProduct = 0;
    int man = 0;

    statFile.open("sales.txt");

    while (numProduct < 100 && statFile >> productNumber >> productName >> price >> soldNumber)
    {
        Selling *aSelling = new Selling;
        aSelling->productNumber = productNumber;
        aSelling->name = productName;
        aSelling->price = price;
        aSelling->soldNumber = soldNumber;
        aSelling->totalRevenue[] = aSelling->price * aSelling->soldNumber;
        productArray[numProduct++] = aSelling;

        //cout << aSelling->productNumber<< " " << aSelling->name << " " << aSelling->price << " " << aSelling->soldNumber << " " << aSelling->totalRevenue << endl;
    }

    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            if (aSelling->totalRevenue[i] > aSelling->totalRevenue[j]) {
                man = aSelling->totalRevenue[i];
                aSelling->totalRevenue[i] = aSelling->totalRevenue[j];
                aSelling->totalRevenue[i] = man;
            }
        }
    }

    for (int i = 0; i < 2; i++) {
        cout << "The top selling product is " << aSelling->name << "with total sales of " << aSelling->totalRevenue[i] << endl;
        cout << "The second top selling product is " << aSelling->name << "with total sales of " << aSelling->totalRevenue[i - 1] << endl;
    }
}

And there is an unexpected expression error at the line aSelling->totalRevenue[] = aSelling->price * aSelling->soldNumber; which I don't understand.

There is some confusion on the arrays to sort:

  • you should define totalRevenue as a double , not an array of doubles,
  • should be sort the first numProduct elements of the array productArray based on the criteria totalRevenue and name , the order is determined by the comparison operator used. Only compare the second criteria if the first criteria give equal values.

Here is a modified version:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

// Structure to hold statistics
struct Selling {
    int productNumber;
    string name;
    double price;
    int soldNumber;
    double totalRevenue;
};

int compareSellings(const Selling *a, const Selling *b) {
    // sort in decreasing order of totalRevenue
    if (a->totalRevenue > b->totalRevenue) return -1;  // a comes before b
    if (a->totalRevenue < b->totalRevenue) return +1;  // b comes before a
    // sort in increasing order of name for the same totalRevenue
    if (a->name < b->name) return -1;  // a comes before b
    if (a->name > b->name) return +1;  // b comes before a
    return 0;
}

int main() {
    ifstream statFile;
    string productName;
    double price;
    int productNumber, soldNumber;
    Selling *productArray[100];
    int numProduct = 0;

    statFile.open("sales.txt");

    while (numProduct < 100 && statFile >> productNumber >> productName >> price >> soldNumber) {
        Selling *aSelling = new Selling;
        aSelling->productNumber = productNumber;
        aSelling->name = productName;
        aSelling->price = price;
        aSelling->soldNumber = soldNumber;
        aSelling->totalRevenue = price * soldNumber;
        productArray[numProduct++] = aSelling;

        //cout << aSelling->productNumber<< " " << aSelling->name << " " 
        //     << aSelling->price << " " << aSelling->soldNumber << " " 
        //     << aSelling->totalRevenue << endl;
    }

    for (int i = 0; i < numProduct; i++) {
        for (int j = 0; j < numProduct; j++) {
            if (compareSellings(productArray[i], productArray[j]) > 0) {
                Selling *aSelling = productArray[i];
                productArray[i] = productArray[j];
                productArray[j] = aSelling;
            }
        }
    }

    cout << "The top selling product is " << productArray[0]->name
         << ", with total sales of " << productArray[0]->totalRevenue << endl;
    cout << "The second selling product is " << productArray[1]->name
         << ", with total sales of " << productArray[1]->totalRevenue << endl;

    return 0;
}

Further remarks:

  • you might use a more efficient sorting method
  • you should free the allocated objects
  • you should use <algorithms> and dynamic arrays of objects instead of allocating the objects with new and manipulating naked pointers.
  • you should handle exceptions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM