C++ Sort & Operator Overloading < (less than)

I have a vector of pointers that point to Objects(string name, int score).

I want it to sort the objects based on score, if object 1 and object 2 have the same score then sort based on name.

This is using the <sort> function.

Lmk if you need additional information.

Def: inline bool operator<(HighScore& Obj2);

1
2
3
4
5
6
7
8
inline bool HighScore::operator<(HighScore& Obj2){
  if(this->getScore() < Obj2.getScore()){
    return true;
  }
  else if((this->getScore() == Obj2.getScore())&&(this->getName() < Obj2.getName())){
    return true;
  }
}


std::sort(compareVec.begin(), compareVec.end());

I edited the code, and while it compiles, it still does not sort.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  HighScore c1("Lawrence", 2300);
  HighScore c2("Mark", 2750);
  HighScore c3("Chris", 2105);
  HighScore c4("Bob", 3120);
  HighScore c5("Kyle", 2300);
  HighScore c6("Trevor", 1785);
  HighScore c7("Josh", 2850);

  HighScore oManager;

  oManager.pushObjectVector(c1);
  oManager.pushObjectVector(c2);
  oManager.pushObjectVector(c3);
  oManager.pushObjectVector(c4);
  oManager.pushObjectVector(c5);
  oManager.pushObjectVector(c6);
  oManager.pushObjectVector(c7);
Last edited on
Add return false between lines 7 and 8 of your comparsion function. And it is better to make comparsion operators non-members
Hi,

What do you mean non members? I soley need this to sort objects. It still does not sort however:

1
2
3
4
5
6
7
8
9
10
11
inline bool HighScore::operator<(HighScore& Obj2){
  if(this->getScore() < Obj2.getScore()){
    return true;
  }
  else if((this->getScore() == Obj2.getScore())&&(this->getName() < Obj2.getName())){
    return true;
  }
  else{ 
    return false;
  }
Your vector contains not Highscore objects, but pointers to them. You should provide custom comparsion function to std::sort (as you cannot overload less operator for pointers)

What do you mean non members?
Something like that:
1
2
3
4
5
6
7
8
9
10
11
#include <tuple>
//...
bool operator<(HighScore& lhs, HighScore& rhs) //Should be const refs but you need to make your class const correct first
{
    return std::make_tuple(lhs.getScore(), lhs.getName()) < 
           std::make_tuple(rhs.getScore(), rhs.getName());
}

//passin custom comparsion function making use of overloaded operator
std::sort(compareVec.begin(), compareVec.end(), [](HighScore* lhs, HighScore* rhs)
            {return *lhs < *rhs;});
Last edited on
I'm sry this is a little advanced for me, is there a way I can fix it with my current code? or more or less close to what I have written? I need to understand the baseline basics before using more advanced alternatives.
Last edited on
Ok. To make it closer:
Have comparsion function as it is now.
Create another function:
1
2
3
4
bool  p_compare(HighScore* lhs, HighScore* rhs)
{
    return *lhs < *rhs;
}

Use sorting like that:
std::sort(compareVec.begin(), compareVec.end(), p_compare);
I get :

 
error C3867: 'HighScore::p_compare': function call missing argument list; use '&HighScore::p_compare' to create a pointer to member


Because of the sort function.
p_compare should be standalone function, do not make it member.

I just noticed that you have a vector of Comparable pointers. You should either have vector of HighScore pointers of have comparsion operator (and comparsion function) defined for Comparable objects.
Last edited on
Hi,

So you want me to take the comparable * vector and make it just Highscore? what If I had multiple classes and wanted 1 vector of pointers to combine them all, wouldn't the way to that be comparable * vector?

Also I made compare a non member and this is the error I got, I am assuming its related to comparable* vs Highscore * of vectors:

 
 error C2664: 'bool (HighScore *,HighScore *)' : cannot convert parameter 2 from 'Comparable *' to 'HighScore *'


Btw I appreciate the time you are taking to help me out.

Ty
what If I had multiple classes and wanted 1 vector of pointers to combine them all,
By storing them in vector of comparable pointers, you lose type information and now have to use whichever Comparable has to offer (member functions and data). That means, you cannot use any member introduced in HighScore.

Error you see isrelated to that: compiler cannot downcast safely.
Topic archived. No new replies allowed.