3

I have C++ code. There is a vector of class. I need to sort this vector in a bit custom way.

Here is my class:

class Sales
{
   public:
      string customer_name;
      string category;
      string aircraft;
      string day;
      string time;
      int week;
      int ticket_price;
      string payment;

   public:
      Sales () { 
         customer_name = "";
         category = "";
         aircraft = "";
         day = "";
         time = "";
         week = 0;
         ticket_price = 0;
         payment = "";
      }

      Sales (string f_cat, string airc, string xday, string xtime, int xweek) {
         customer_name = "";
         category = f_cat;
         aircraft = airc;
         day = xday;
         time = xtime;
         week = xweek;
         ticket_price = 0;
         payment = "";
      }  
};

Lets say we have:

vector <Sales> list;

Imagine that list has got populated record And I want to get this sorted like below logic:

sort_string = day + time + week + category + aircraft;

Example records:

Sunday, 12:00, 1, Comm, b777
Monday, 10:00, 1, Comm, b777
Monday, 10:00, 1, Comm, a321
Monday, 12:00, 1, Comm, a321
Friday, 09:00, 1, Comm, a321

Expected Sort:

Monday, 10:00, 1, Comm, a321
Monday, 10:00, 1, Comm, b777
Monday, 12:00, 1, Comm, a321
Friday, 09:00, 1, Comm, a321
Sunday, 12:00, 1, Comm, b777

Is this possible? if Yes, then how?

Thanks.

2 Answers 2

7

Yes, it is.

Define

bool Sales::operator<( const Sales& other ) // member

or

bool operator<( const Sales& lhs, const Sales& rhs) // global

and use std::sort.


Example with only two int variables, as they are the easiest ones:

bool operator<( const Sales& lhs, const Sales& rhs)
{
    if( lhs.week < rhs.week ) 
         return true;
    else if( lhs.week > rhs.week )  
         return false;

    if( lhs.price < rhs.price ) 
         return true;
    else if( lhs.price > rhs.price )  
         return false;

    // ...

    return false;
}

Of course, your definition will be more complicated, because the other fields are more complicated for comparing (the weekday for example), but I hope you get the point.

Then:

#include <algorithm>
// ...
std::sort( list.begin(), list.end() );

By the way, list is bad name for variable, as it may conflict with std::list in some cases.

Sign up to request clarification or add additional context in comments.

16 Comments

how str::sort will know which variables to use in which order?
You define it in the body of the functions listed by Kiril above.
@ChrisL I am not familiar with this. Could you provide me an example?
This isn't exactly what you need but an example. If you wanted to sort by week, then you'd define bool operator<( const Sales& lsh, const Sales& rhs) { return (lhs.week < rhs.week); } //ie if the week is less on the left than the right return true else false. This logic is used by std::sort to order your container
@kiril looks good to me - saves me writing it in a comment, i've upvoted your answer now.
|
0

if you will implement boolean function as member it must be const type

    bool operator<( const Sales& rhs) const{ 
        if (customer_name < rhs.customer_name ) return true; 
        if (category < rhs.category ) return true; 
        return false;
    }

See working example

4 Comments

Why "must" ? Maybe "should"?
It was not compiling for me until I put it.
@BorisIvanov this is what I get when I compile test.cpp:73: error: a function-definition is not allowed here before ':' token because of for loops
/usr/include/c++/4.8/bits/stl_algo.h:2269:19: error: passing ‘const Sales’ as ‘this’ argument of ‘bool Sales::operator<(const Sales&)’ discards qualifiers [-fpermissive] while (__pivot < *__last)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.