For example you can define the operator < for the class.
Here is a demonstrative program
#include <iostream>
#include <tuple>
#include <list>
class Info
{
public:
Info( int wi, int bi, int di ) : w( wi ), b( bi ), d( di )
{
}
friend bool operator <( const Info &, const Info & );
friend std::ostream & operator << ( std::ostream &, const Info &info )
{
return std::cout << "{ " << info.d << ", " << info.w << ", " << info.b << " }";
}
private:
int w = -1;
int b = -1;
int d = -1;
};
bool operator <( const Info &a, const Info &b )
{
return std::tie( a.d, a.w, a.b ) < std::tie( b.d, b.w, b.b );
}
int main()
{
std::list<Info> lst = { { 1, 2, 3 }, { 1, 1, 2 }, { 1, 3, 2 }, { 2, 1, 2 }, { 2, 2, 3 } };
lst.sort();
for ( const auto &item : lst ) std::cout << item << '\n';
return 0;
}
Its output is
{ 2, 1, 1 }
{ 2, 1, 3 }
{ 2, 2, 1 }
{ 3, 1, 2 }
{ 3, 2, 2 }
That is you can use the standard function std::tie
that does the all work.
Or with the container std::vector
#include <iostream>
#include <tuple>
#include <vector>
#include <iterator>
#include <algorithm>
class Info
{
public:
Info( int wi, int bi, int di ) : w( wi ), b( bi ), d( di )
{
}
friend bool operator <( const Info &, const Info & );
friend std::ostream & operator << ( std::ostream &, const Info &info )
{
return std::cout << "{ " << info.d << ", " << info.w << ", " << info.b << " }";
}
private:
int w = -1;
int b = -1;
int d = -1;
};
bool operator <( const Info &a, const Info &b )
{
return std::tie( a.d, a.w, a.b ) < std::tie( b.d, b.w, b.b );
}
int main()
{
std::vector<Info> v = { { 1, 2, 3 }, { 1, 1, 2 }, { 1, 3, 2 }, { 2, 1, 2 }, { 2, 2, 3 } };
std::sort( std::begin( v ), std::end( v ) );
for ( const auto &item : v ) std::cout << item << '\n';
return 0;
}
You can use a lambda expression provided that you have access to the data members of the class for example through member functions.
#include <iostream>
#include <tuple>
#include <vector>
#include <iterator>
#include <algorithm>
class Info
{
public:
Info( int wi, int bi, int di ) : w( wi ), b( bi ), d( di )
{
}
int w = -1;
int b = -1;
int d = -1;
};
std::ostream & operator << ( std::ostream &, const Info &info )
{
return std::cout << "{ " << info.d << ", " << info.w << ", " << info.b << " }";
}
int main()
{
std::vector<Info> v = { { 1, 2, 3 }, { 1, 1, 2 }, { 1, 3, 2 }, { 2, 1, 2 }, { 2, 2, 3 } };
std::sort( std::begin( v ), std::end( v ),
[]( const auto &a, const auto &b )
{
return std::tie( a.d, a.w, a.b ) < std::tie( b.d, b.w, b.b );
} );
for ( const auto &item : v ) std::cout << item << '\n';
return 0;
}