1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cctype>
using namespace std;
string toUpper( const string s )
{
string upper = s;
for ( int i = 0; i < s.size(); i++ ) upper[i] = toupper( upper[i] );
return upper;
}
struct Person
{
string firstname;
string lastname;
float length; // what?
Person( string first, string last, float len ) : firstname( first ), lastname( last ), length( len ) {}
};
bool cmp( Person A, Person B )
{
if ( toUpper( A.lastname ) == toUpper( B.lastname ) ) return ( toUpper( A.firstname ) < toUpper( B.firstname ) );
else return ( toUpper( A.lastname ) < toUpper( B.lastname ) );
}
ostream &operator<< ( ostream &stream, Person &p ) { stream << p.firstname << " " << p.lastname; return stream; }
int main()
{
vector<Person> people;
people.push_back( Person( "David", "Small", 1.5 ) );
people.push_back( Person( "David", "LARGE", 2.2 ) );
people.push_back( Person( "Bob" , "Small", 1.3 ) );
people.push_back( Person( "Alice", "Large", 1.6 ) );
people.push_back( Person( "ABC" , "xyz" , 1.7 ) );
people.push_back( Person( "XYZ" , "abc" , 1.6 ) );
sort( people.begin(), people.end(), cmp );
for ( int i = 0; i < people.size(); i++ ) cout << people[i] << endl;
}
|