473,396 Members | 2,092 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,396 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 1092
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.