With only one field is easy:
bool operator<(const MidiMsg &midiMsg) const {
return (mPosition < midiMsg.mPosition);
}
But what if I need to order first by 1 member, than by another?
Tried this:
bool operator<(const MidiMsg &midiMsg) const {
return (mPosition < midiMsg.mPosition && mId < midiMsg.mId);
}
But it seems that doesn't works.
Here's a working example:
#include <iostream>
#include <deque>
#include <algorithm>
struct MidiMsg {
int mPosition;
int mId;
bool operator<(const MidiMsg &midiMsg) const {
return (mPosition < midiMsg.mPosition && mId < midiMsg.mId);
}
};
int main() {
std::cout.precision(10);
std::deque<MidiMsg> mNotes;
MidiMsg note;
note.mPosition = 0;
note.mId = 1;
mNotes.push_back(note);
note.mPosition = 44100;
note.mId = 1;
mNotes.push_back(note);
note.mPosition = 0;
note.mId = 0;
mNotes.push_back(note);
note.mPosition = 44100;
note.mId = 0;
mNotes.push_back(note);
note.mPosition = 0;
note.mId = 2;
mNotes.push_back(note);
note.mPosition = 44100;
note.mId = 2;
mNotes.push_back(note);
std::sort(mNotes.begin(), mNotes.end());
for (size_t index = 0; index < mNotes.size(); index++) {
std::cout << mNotes[index].mPosition << " - " << mNotes[index].mId << std::endl;
}
}
Which outputs:
0 - 1
0 - 0
44100 - 1
44100 - 0
0 - 2
44100 - 2
and not:
0 - 0
0 - 1
0 - 2
44100 - 0
44100 - 1
44100 - 2