I cannot figure out how to sort a vector of objects by one of the member variables called VIN, which is of data type string
. I am to either perform a bubble sort or a selection sort. I am most familiar with a bubble sort, so that's the direction I attempted to take. However, I am aware that I need to compare both of the strings by either making them all uppercase or lower case. Keep in mind, the strings also will have number characters in them as well.
So my question is: 1) How to I convert a string either all lowercase or all ASCII numbers and 2) What data type should the temp variable be (data type of the class or some other data type).
Here is the (incomplete) code I have so far:
void sortInventory(vector<Vehicle> &carList)
{
bool swap;
string temp;
do
{
swap = false;
for (int count = 0; count < carList.size(); count++)
{
if (carList[count].getVIN() > carList[count + 1].getVIN())
{
temp = carList[count].getVIN();
carList[count].getVIN() = carList[count + 1].getVIN();
carList[count + 1].getVIN() = temp;
swap = true;
}
}
} while (swap);
}
Here is my class declaration:
class Vehicle
{
private:
string VIN;
public:
string getVIN();
void setVIN(string);
};
Here is my class implementation:
string Vehicle::getVIN()
{ return VIN; }
void Vehicle::setVIN(string input)
{ VIN = input; }
Thanks!