0

I have song list, where song is an object, songName and songArtist are both song's attributes; I need to order them alphabetically according to name or artist, depends on the user; I want to overload > operator to do so, but I wanted to know if I can kind of add a flag to identify if user wants to order them according to the name of the song or the artist name.

bool Song::operator > (const Song& s, const bool& flag){
    if(flag)
      return songName> s.songName;
    else
      return songArtist> s.songArtist;
}

And if so, how can I make the comparison? I mean, where would the flag be if my evaluation is like if(song>s.song)?

lissethamc
  • 79
  • 1
  • 3
  • 12
  • *I want to overload < operator to do so* -- No, don't do that. Reserve that operator for what it actually is supposed to do and mean, not for convenience for sorting -- I see this horrible mistake (even though it may work) done so many times. Note that this also includes your overload of `>`. Don't use it just to make `sort` happy. Instead, use the `std::sort` function that has 3 arguments, the third being a comparison function object or lambda. – PaulMcKenzie Sep 22 '19 at 00:40
  • Thank you, but we're practicing sort algorithms so I have to compare them somehow and I thought this could be an easy way. Is this bad practice when you need to compare many object's attributes? – lissethamc Sep 22 '19 at 00:46
  • 2
    You can still compare objects without polluting the object itself. How does `std::sort` work (the 3 argument version)? Also [read this link](https://stackoverflow.com/questions/24650626/how-to-implement-classic-sorting-algorithms-in-modern-c). Note that there is a comparison parameter that is provided to each of those implementations. As to being bad practice, IMO it is bad practice to sacrifice operators just to make `sort` happy, which is what you're doing. An object should have an unambiguous, well thought-out reason for one object "being less than" another. – PaulMcKenzie Sep 22 '19 at 00:48
  • I'm checking it then, thank you. – lissethamc Sep 22 '19 at 00:53
  • 1
    Note that the answer you accepted uses the 3rd parameter that I mentioned in my earlier comment (the lambda function). – PaulMcKenzie Sep 22 '19 at 01:13
  • As a note, comparison operators typically don't allow you to specify the comparison algorithm. The choice of algorithm for operators is typically an invariant, often based on one or more of the entity's components and intended to work the way people expect that sort of comparison to work. It might, however, wrap a call to a different comparison tool, though. In this case, most people would likely expect `<` and `>` to depend on the song's title, and it could, e.g., call a `Song::compare()` while specifying a "title" flag. – Justin Time - Reinstate Monica Sep 22 '19 at 02:47

1 Answers1

3

No, you can't. operator> is a binary operator; it has to take exactly two arguments (including *this if present). There is no spare slot for the flag.

If your intention is to make std::sort work, first note that > isn't used at all (< is) unless you explicitly ass a comparator like std::greater<>{}. Then you can just provide a custom comparator:

std::sort(vec.begin(), vec.end(),
          [flag](const auto& a, const auto& b) { /* ... */ });
L. F.
  • 19,445
  • 8
  • 48
  • 82