how to sort an array of objects?

M

mrNoob

sorry iam only coding for a month

i want to sort "personS" on there "score". how can i do this using
the code below ? ,, i keep having errors :(

class person
{
public:
person();
// etc...
private:
int age;
int score; // sort on score
// etc...
};

int main()
{
person *personS; // to be sorted
personS = new person[10];
sort(personS, personS+10, ??? ); // ? QUESTION HERE ?

return(1);
}

plz dont be too absract :/
 
T

Thomas Matthews

mrNoob said:
sorry iam only coding for a month

i want to sort "personS" on there "score". how can i do this using
the code below ? ,, i keep having errors :(

class person
{
public:
person();
// etc...
Add a function to compare two persons by score:
bool scored_less_than(const person& p} const
{return score < p.score;}
This function will be used below in sorting.

private:
int age;
int score; // sort on score
// etc...
};
Create a global function to compare two persons by age:
bool Person_Compare_By_Score(const person& p1,
const person& p2)
{
return p1.scored_less_than(p2);
}

int main()
You may want to be explicit:
int main(void)

{
person *personS; // to be sorted
personS = new person[10];
sort(personS, personS+10, ??? ); // ? QUESTION HERE ?
Pointers are not required here:
const unsigned int MAX_PERSONS = 10;
person personS[MAX_PERSONS];
/* Add code to initialize the persons */

/* Here is the code to sort by score: */
sort(personS, personS + MAX_PERSONS,
Person_Compare_By_Score);

The function is passed to the sort routine. See above.
return(1);
return EXIT_SUCCESS; /* See said:
}

plz dont be too absract :/
Spell out your abbreviations. This will give others
a more educated opinion of you.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
A

Aggro

Thomas said:
Add a function to compare two persons by score:
bool scored_less_than(const person& p} const

That should be:
bool scored_less_than(const person& p) const

And op also needs to include this:
#include <algorithm>


But I have also an extra question:
Can you do the same for items in std::vector, or do you have to convert
vector to some other format before sorting can be done?

Any recommendations how to do it?
 
M

Mike Wahler

Aggro said:
That should be:
bool scored_less_than(const person& p) const

And op also needs to include this:
#include <algorithm>


But I have also an extra question:
Can you do the same for items in std::vector,

Yes. std::sort works with any container which supports
random access iterators (pointer types qualify as such,
that's why it works with arrays).
or do you have to convert
vector to some other format before sorting can be done?
No.


Any recommendations how to do it?

Pass iterators to the vector instead of array element addresses.

std::vector<person> pv;

/* (populate vector) */

std::sort(pv.begin(), pv.end(), Person_Compare_By_Score);

-Mike
 
M

Mike Wahler

Thomas Matthews said:
You may want to be explicit:
int main(void)

Nit: In C++, empty parameter list
*means* no parameters (unlike C).

Also many consider the empty list more
idiomatic for C++.

YMMV.

-Mike
 
H

Hardy

oh, Mike, can you explain what the means of empty parameter in C? I just can
not tell the difference.

Thank you!
 
A

Alex Vinokur

mrNoob said:
sorry iam only coding for a month

i want to sort "personS" on there "score". how can i do this using
the code below ? ,, i keep having errors :(

class person
{
public:
person();
// etc...
private:
int age;
int score; // sort on score
// etc...
};

int main()
{
person *personS; // to be sorted
personS = new person[10];
sort(personS, personS+10, ??? ); // ? QUESTION HERE ?

return(1);
}

plz dont be too absract :/


Look at Appendix D.4 at
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1487.pdf
 
K

Karl Heinz Buchegger

Mike said:
Nit: In C++, empty parameter list
*means* no parameters (unlike C).

In a function definition an empty parameter list
means the same thing in C and C++: no parameters.

In a function decalration (aka protoype) an empty
parameter list means different things in C++ and C

C++: no parameters
C : unknown number of parameters
 
M

Mike Wahler

Hardy said:
oh, Mike, can you explain what the means of empty parameter in C? I just can
not tell the difference.

<C>
It means 'parameters not specified', whereas a
'void' parameter specifically means 'no parameters'.
</C>

In C++, a 'void' parameter and an empty parameter
list mean the same: no parameters.

-Mike
 

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

No members online now.

Forum statistics

Threads
474,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top