473,378 Members | 1,493 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,378 software developers and data experts.

how to sort an array of objects?

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 :/
Jul 22 '05 #1
8 1628
mrNoob wrote:
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 <cstdlib> */ }

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

Jul 22 '05 #2
Thomas Matthews wrote:
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?
Jul 22 '05 #3
"Aggro" <sp**********@yahoo.com> wrote in message
news:Mj***************@read3.inet.fi...
Thomas Matthews wrote:
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,


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

Jul 22 '05 #4
"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:Xm**************@newssvr16.news.prodigy.com.. .
mrNoob wrote:

int main()

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
Jul 22 '05 #5
oh, Mike, can you explain what the means of empty parameter in C? I just can
not tell the difference.

Thank you!

"Mike Wahler" <mk******@mkwahler.net> дÈëÓʼþ
news:3e*****************@newsread2.news.pas.earthl ink.net...
"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:Xm**************@newssvr16.news.prodigy.com.. .
mrNoob wrote:

int main()

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

Jul 22 '05 #6

"mrNoob" <br*******@hotmail.com> wrote in message news:5e**************************@posting.google.c om...
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/wg2...2003/n1487.pdf
--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 22 '05 #7
Mike Wahler wrote:

"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:Xm**************@newssvr16.news.prodigy.com.. .
mrNoob wrote:

int main()

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


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

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #8
"Hardy" <yh********@hotmail.com> wrote in message
news:ca***********@mail.cn99.com...
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
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
8
by: laniik | last post by:
Hi. I have a problem using STL's built in sort that seems impossible to get around. if i have: -------------------------------- struct object { int val; }
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
7
by: Daniel | last post by:
does C# have any collection objects that support sort functionality so that I dont have to write my own sorting algorithm?
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
3
by: fdu.xiaojf | last post by:
Hi, It seems that an array acts like an list very much, except it doesn't have a method sort. Regards,
2
by: John Devlon | last post by:
Hi, I've created my own class containing a few properties like name, zip-code and savings. At some point i'm storing several objects in an array. Does anyone know how to sort the array using...
0
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.