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

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » list » list sort 
17.14.5.Sorting user-defined objects in list by its properties
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>

using namespace std;

class Player
{
   public:
   Playerconst 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 heightint& 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::Playerconst 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::heightint& 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;
   heightfeet, inches );
   string namefirst_name() );
   name += " ";
   name += last_name();
   cout << setw20 << 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_heightconst Player& lhs, const Player& rhs )
return lhs.height() > rhs.height()}

inline
bool greater_field_goal_percentageconst Player& lhs,
   const Player& rhs )
return lhs.field_goal_percentage()
   > rhs.field_goal_percentage()}

int main( )
{
   list<Player> players;

   players.push_backPlayer"A""F"780.422 ) );
   players.push_backPlayer"B""G"720.392 ) );
   players.push_backPlayer"C""H"850.555 ) );
   players.push_backPlayer"D""I"900.531 ) );
   players.push_backPlayer"E""J"840.502) );

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

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

   // descending sort by field goal percentage
   players.sortgreater_field_goal_percentage );
   for_eachplayers.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()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.