sort with vector<struct>

O

Oliver Block

Hi,

can anyone if it is enough to place the elements of a structure in a
different order when you want to sort it.

for example sort by name:

struct item 1 {
String name;
int age;
}

or otherwise sort by age:

struct item 2 {
int age;
String name;
}

I never used that routine. Would that work and are there better ways?

bye,

Oliver
 
P

peter.koch.larsen

Oliver Block skrev:
Hi,

can anyone if it is enough to place the elements of a structure in a
different order when you want to sort it.

for example sort by name:

struct item 1 {
String name;
int age;
}

or otherwise sort by age:

struct item 2 {
int age;
String name;
}

It is not.
I never used that routine. Would that work and are there better ways?

bye,

Oliver
You need to write a predicate "less" that compares the struct according
to your requirement. Google around - there are lots of examples.

/Peter
 
O

Oliver Block

You need to write a predicate "less" that compares the struct according
to your requirement. Google around - there are lots of examples.


thank you! I added a class cmp which implements just Operator().

bye,

Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top