Sorting user-defined objects in list by its properties : list sort « list « C++ Tutorial






#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>

using namespace std;

class Player
{
   public:
   Player( const string& first_name = "John",
           const string& last_name = "Doe", 
           int height = 60,
           float field_goal_percentage = 10 ); 
           
   bool operator<( const Player& rhs ) const;

   float field_goal_percentage() const;
   string first_name() const;
   int height() const;  
   void height( int& feet, int& inches ) const;
   string last_name() const;
   void print() const;

   private:
   float field_goal_percentage_;
   string first_name_;
   int height_;   
   string last_name_;
};

inline
Player::Player( const string& first_name, const string& last_name,
   int height, float field_goal_percentage )
   : field_goal_percentage_( field_goal_percentage ),
     first_name_( first_name ), height_( height ),
     last_name_( last_name )
{} 
inline
float Player::field_goal_percentage() const
{  return field_goal_percentage_; }

inline
string Player::first_name() const
{  return first_name_; }

inline
int Player::height() const  // in inches
{  return height_; }

inline
void Player::height( int& feet, int& inches ) const
{
   feet = height() / 12;
   inches = height() % 12;
}

inline
string Player::last_name() const
{  return last_name_; }

void Player::print() const
{
   int feet, inches;
   height( feet, inches );
   string name( first_name() );
   name += " ";
   name += last_name();
   cout << setw( 20 ) << left << name << feet << " feet " << inches
      << " inches   Field goal percentage: "
      << field_goal_percentage() << endl;
}

inline
bool Player::operator<( const Player& rhs ) const
{ return last_name() < rhs.last_name(); }

inline
bool greater_height( const Player& lhs, const Player& rhs )
{ return lhs.height() > rhs.height(); }

inline
bool greater_field_goal_percentage( const Player& lhs,
   const Player& rhs )
{ return lhs.field_goal_percentage()
   > rhs.field_goal_percentage(); }

int main( )
{
   list<Player> players;

   players.push_back( Player( "A", "F", 78, 0.422 ) );
   players.push_back( Player( "B", "G", 72, 0.392 ) );
   players.push_back( Player( "C", "H", 85, 0.555 ) );
   players.push_back( Player( "D", "I", 90, 0.531 ) );
   players.push_back( Player( "E", "J", 84, 0.502) );

   // sort by name
   players.sort();
   for_each( players.begin(), players.end(),mem_fun_ref( &Player::print ) );

   // descending sort by height
   players.sort( greater_height );
   for_each( players.begin(), players.end(),mem_fun_ref( &Player::print ) );

   // descending sort by field goal percentage
   players.sort( greater_field_goal_percentage );
   for_each( players.begin(), players.end(),mem_fun_ref( &Player::print ) );
}








17.14.list sort
17.14.1.Sort a list
17.14.2.sort objects in a list
17.14.3.sort list with user-defined objects with greater
17.14.4.Sorting a List of Integers in Ascending and Descending Order
17.14.5.Sorting user-defined objects in list by its properties
17.14.6.list.sort()