Custom sort in C++ adding attributes
Hi,
Hi,
If you are learning the ways of sort, then yes it would be complicated. Just add every condition in the swap instead of just
Bill,
Bubble sort is not as quick as quick sort.
You should try that and yes, as software synthesis said, there is simply no need to re-invent the wheel. There is a sort function in algorithm. All you need to do is define in a comparator function.
To know how to use a comparator function, you can read this.
Bill,
It is very easy. Just like any other ordinary function. Its return type is blue and takes 2 parameters a and b.
Suppose a and b are 2 consecutive elements of the vector.
If a and b is in proper placements i.e a<b (for ascending) then return true, else return false.
But I have 3 properties to be considered;
Do I need 3 comparator functions? or 1 to do all of them ? Then how many parameter would there be? 6?
You don't need to pass all the properties. Just pass the two objects and access their properties, from within the function. Here is a simple comparator function to help you out
bool comparator(items a,items b)
{
   if(a.cost>b.cost)
       return false;
   if(a.cost<b.cost)
       return true;
   else if(a.weight>b.weight)
       return false;
   else if (a.weight<b.weight)
       return true;
   else if(a.name>b.name)
       return false;
   else
       return true;
}
Try this and I think you shall get it soon.
Thanks a lot Software Synthesis. I got that finally. It's really amazing.