Sorting Name with Quantity HELP!!

I am creating an inventory system that tracks inventory. I need to sort the names of the products alphabetically. And it needs to keep the inventory with it.

For example: If I enter "Banana" for the name and the inventory quantity is "7", and then I enter "Apple" with the inventory of "5", I need to sort it so Apple and 5 come first. Below is the code for the vector that I have so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void sortbyname(vector<Product>& bginventory) //This sorts the products entered by name.  
{
    Product temp;
    int i, j, smalllet;
    int n = bginventory.size();

    for (i = 0; i < n - 1; i++)
    {
        smalllet = i;
        for (j = i + 1; j < n; j++)            //Sorting loop
            if (bginventory.at(j).name < bginventory.at(smalllet).name)
                smalllet = j;

        temp = bginventory.at(i);
        bginventory.at(smalllet).name = bginventory.at(i).name;
        bginventory.at(i).name = smalllet;

    }
}


Please let me know what I'm doing wrong because this current code doesn't give the right output. I believe there is an issue with the last statement.
Last edited on
First, what comment calls "Sorting loop" is not about sorting.
What do lines 9-12 actually do? They find the index of product with "smallest name".


You wan't to move that product from index smalllet to index i, don't you?
What should you do for the product that is currently at index i?

On line 14 you make a copy of product that is currently at index i. You never use that copy.

On line 15 you overwrite the name of product that is at index smalllet. The original name of that product is now lost.

On line 16 you write smalllert, an int value, to name of product at index i. Are names integers?

Lets go back to line 14. You have a copy of name and count, dont' you?
What if you now overwrite entire product at index i with product from index smalllet?
Finally, store the value that was at index i into index smalllet from the 'temp'.
You're a genius. It works 100% now. Here is the new code for anyone interested:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void sortbyname(vector<Product>& bginventory) //This sorts the products entered by name.  
{
    Product temp;
    int i, j, smalllet;
    int n = bginventory.size();

    for (i = 0; i < n - 1; i++)
    {
        smalllet = i;
        for (j = i + 1; j < n; j++)            //Index to find "smallest name"
            if (bginventory.at(j).name < bginventory.at(smalllet).name)
                smalllet = j;

        temp = bginventory.at(i);
        bginventory.at(i)=bginventory.at(smalllet);
        bginventory.at(smalllet)=temp;

    }
}
Good. There are two parts to programming: logic and syntax. What to do and how to do it.
Writing a sort is mostly about logic. What steps are necessary.
Mastering C++ is on the "how to". Other languages require different idioms.

C++ has Standard Library. It has reusable components:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void sortbyname( vector<Product>& bginventory )
{
    int n = bginventory.size();
    for ( i = 0; i < n - 1; i++)
    {
        // find "smallest name" http://www.cplusplus.com/reference/algorithm/min_element/
        auto pos = std::min_element( bginventory.begin() + i + 1,
                bginventory.end(), [](const Product& lhs, const Product& rhs)
                                {return lhs.name < rhs.name;} );
        // iterator to index http://www.cplusplus.com/reference/iterator/distance/
        int smalllet = std::distance( bginventory.begin(), pos );

        if ( smalllet != i ) // http://www.cplusplus.com/reference/algorithm/swap/
                std::swap( bginventory.at(i), bginventory.at(smalllet) );
    }
}

The [](const Product& lhs, const Product& rhs) {return lhs.name < rhs.name;} is a lambda closure. A function object.

However, the library can do better than that. It has std::sort:
1
2
3
4
5
6
void sortbyname( vector<Product>& bginventory )
{
    // http://www.cplusplus.com/reference/algorithm/sort/
    std::sort( bginventory.begin(), bginventory.end(),
        [](const Product& lhs, const Product& rhs) {return lhs.name < rhs.name;} );
}
Registered users can post here. Sign in or register to post.