473,232 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,232 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 1083
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
12
by: pmud | last post by:
Hi, I am using teh following code for sorting the data grid but it doesnt work. I have set the auto generate columns to false. & set the sort expression for each field as the anme of that...
16
by: pmud | last post by:
Hi, I am using teh following code for sorting the data grid but it doesnt work. I have set the auto generate columns to false. & set the sort expression for each field as the anme of that...
1
by: Ahmed Yasser | last post by:
Hi all, i have a problem with the datagridview sorting, the problem is a bit complicated so i hope i can describe in the following steps: 1. i have a datagridview with two columns...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.