471,831 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,831 software developers and data experts.

sorting the value not the key

Hi

which container I can use to do the following,

I have a data file looking like
name age
jack 32
sam 12
sue 44

I need to sort them by age so that it would be

sam 12
jack 32
sue 44

I don't think map will let me sort its values, do I need to something
like this?

class mytype
{
string name;
int age;
public:
mytype(string n, int a){}
}

mytype jack("jack",32);
mytype sam("sam",12);
mytype sue("sue",44);

vector<mytypegroupA;
groupA.push_back(jack);
....

****************
how do I sort groupA by age?
****************

thanks
Aug 24 '06 #1
7 1023
Gary Wessle wrote:
Hi

which container I can use to do the following,

I have a data file looking like
name age
jack 32
sam 12
sue 44

I need to sort them by age so that it would be

sam 12
jack 32
sue 44

I don't think map will let me sort its values, do I need to something
like this?

class mytype
{
string name;
int age;
public:
mytype(string n, int a){}
}

mytype jack("jack",32);
mytype sam("sam",12);
mytype sue("sue",44);

vector<mytypegroupA;
groupA.push_back(jack);
...

****************
how do I sort groupA by age?
****************

thanks
Define an operator< function for two mytype objects, then use
std::sort. See the last example here:

http://www.cppreference.com/cppalgorithm/sort.html

Cheers! --M

Aug 24 '06 #2
Gary Wessle wrote:
Hi

which container I can use to do the following,

I have a data file looking like
name age
jack 32
sam 12
sue 44

I need to sort them by age so that it would be

sam 12
jack 32
sue 44

I don't think map will let me sort its values, do I need to something
like this?

class mytype
{
string name;
int age;
public:
mytype(string n, int a){}
add this here:

int getAge() const { return age; }
}

mytype jack("jack",32);
mytype sam("sam",12);
mytype sue("sue",44);

vector<mytypegroupA;
groupA.push_back(jack);
...

****************
how do I sort groupA by age?
#include <functional>

using namespace std;

typedef const mytype& crMytype;

struct less_age : public binary_function<crMytype, crMytype, bool{
bool operator()(crMytype x, crMytype y)
{ return x.getAge() < y.getAge(); }
};

sort(groupA.begin(), groupA.end(), less_age());

Best regards,

Tom

Aug 24 '06 #3
In article <87************@yahoo.com>, Gary Wessle <ph****@yahoo.com>
wrote:
Hi

which container I can use to do the following,

I have a data file looking like
name age
jack 32
sam 12
sue 44

I need to sort them by age so that it would be

sam 12
jack 32
sue 44

I don't think map will let me sort its values.
Then make the age the key... (You might want to use a multi-map for
this.)
Aug 25 '06 #4
I like your solution best of the 3 but:

Thomas Tutone wrote:
>
add this here:

int getAge() const { return age; }
}


****************
how do I sort groupA by age?

#include <functional>

using namespace std;

typedef const mytype& crMytype;

struct less_age : public binary_function<crMytype, crMytype, bool{
bool operator()(crMytype x, crMytype y)
{ return x.getAge() < y.getAge(); }
};
of course you wouldn't put using namespace std; in a header. But you
might want this functor in a header. You might even nest it in the
class.

Now is there a binder for this? I don't know, let's try to create one:

template < typename T >
struct less_mem_fun_ref : public binary_function< const T & t1, const T
& t2, bool >
{
typedef bool (* T::Pred )(void); // sorry I can't remember the
syntax
Pred pred;
less_mem_fun_ref( Pred p ) : pred( p ) {}

bool operator() ( const T& t1, const T& t2 )
{
return t1.pred() < t2.pred();
}
};

then go through the usual process of writing a function to obtain one.
We might also template the use of operator<.

How to do it with boost, anyone?
sort(groupA.begin(), groupA.end(), less_age());
With mine you'd put in make_less_mem_fun_ref( &Mytype::getAge ) as the
last parameter. Of course you need to include <algorithmas well to
use sort.

With mine you'll write the functor once only then invoke it with
different classes, different members etc.

Aug 25 '06 #5
"Daniel T." <da******@earthlink.netwrites:
In article <87************@yahoo.com>, Gary Wessle <ph****@yahoo.com>
wrote:
>Hi

which container I can use to do the following,

I have a data file looking like
name age
jack 32
sam 12
sue 44

I need to sort them by age so that it would be

sam 12
jack 32
sue 44

I don't think map will let me sort its values.

Then make the age the key... (You might want to use a multi-map for
this.)
if you make the age the key, what happens if you have 2 names with the
same age, how can that be sorted out in the map, I think the new entry
will over-write the old. i.e
sam 15
jack 15

if you make the value the key then we have

map["15"]= "sam";
map["15"]= "jack";

now sam does not exist any more (a lost record)....

Aug 26 '06 #6
Gary Wessle wrote:
"Daniel T." <da******@earthlink.netwrites:
>In article <87************@yahoo.com>, Gary Wessle <ph****@yahoo.com>
wrote:
>>Hi

which container I can use to do the following,

I have a data file looking like
name age
jack 32
sam 12
sue 44

I need to sort them by age so that it would be

sam 12
jack 32
sue 44

I don't think map will let me sort its values.

Then make the age the key... (You might want to use a multi-map for
this.)

if you make the age the key, what happens if you have 2 names with the
same age, how can that be sorted out in the map, I think the new entry
will over-write the old. i.e
sam 15
jack 15

if you make the value the key then we have

map["15"]= "sam";
map["15"]= "jack";

now sam does not exist any more (a lost record)....
Please notice that Daniel told you to consider std::multimap instead of
std::map. Then, key-collisions are not an issue.
Best

Kai-Uwe Bux
Aug 26 '06 #7
In article <87************@yahoo.com>, Gary Wessle <ph****@yahoo.com>
wrote:
"Daniel T." <da******@earthlink.netwrites:
In article <87************@yahoo.com>, Gary Wessle <ph****@yahoo.com>
wrote:
Hi

which container I can use to do the following,

I have a data file looking like
name age
jack 32
sam 12
sue 44

I need to sort them by age so that it would be

sam 12
jack 32
sue 44

I don't think map will let me sort its values.
Then make the age the key... (You might want to use a multi-map for
this.)

if you make the age the key, what happens if you have 2 names with the
same age, how can that be sorted out in the map, I think the new entry
will over-write the old. i.e
sam 15
jack 15

if you make the value the key then we have

map["15"]= "sam";
map["15"]= "jack";

now sam does not exist any more (a lost record)....
Use a multimap and it won't be a problem. If you don't want to change
the age to the key then here is another idea...

typedef pair< string, int Person;
typedef map< string, int AgeMap;

bool second_less( Person left, Person right ) {
return left.second < right.second;
}

void fn( const AgeMap& ageMap )
{
vector< Person temp( ageMap.begin(), ageMap.end() );
sort( temp.begin(), temp.end(), &second_less );
// at this point, temp will have all the elements of ageMap,
// sorted by age.
}
Aug 26 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.