sorting array of objects...

S

spl

I have an array of objects. I want to sort the array of a
given numeric value that exists in each of the objects. Suppose the
array of objects is called student and the numeric value I want to
sort the array based on the member is called "rollno".
ex:
struct student{
int rollno;
int name;
int status;
};
student obj[10]
here, I have to sort obj of 10objects, based on rollno.

Please give your suggestion for any fast method?
 
M

Malcolm McLean

spl said:
I have an array of objects. I want to sort the array of a
given numeric value that exists in each of the objects. Suppose the
array of objects is called student and the numeric value I want to
sort the array based on the member is called "rollno".
ex:
struct student{
int rollno;
int name;
int status;
};
student obj[10]
here, I have to sort obj of 10objects, based on rollno.

Please give your suggestion for any fast method?

int compfunct(const void *e1, const void *e2)
{
const student *s1 = e1;
const student *s2 = e2;

return s1->rollno - s2->rollno;
}

in main body of code:

qsort(obj, 10, sizeof(student), compfunct);
 
B

Ben Bacarisse

spl said:
I have an array of objects. I want to sort the array of a
given numeric value that exists in each of the objects. Suppose the
array of objects is called student and the numeric value I want to
sort the array based on the member is called "rollno".

You've been pointer to qsort and this is the right advice, but...
ex:
struct student{
int rollno;
int name;
int status;
};
student obj[10]

This looks like C++ not C. If youy are to use C++ (and I take it this
is homework) you might be expected to do it the C++ way. To find out
about this, post in comp.lang.c++.

[And why is the name an int?]
 
R

Richard Heathfield

Ben Bacarisse said:
struct student{
int rollno;
int name;
int status;
};
student obj[10]

This looks like C++ not C.
[And why is the name an int?]

"I was born at 04 hundred hours on 1/3/22
Somerset House was notified
'A little 7874322 (Column 159)'.
Baptism later certified
(Parish Register Page 183):
'I name this child . . . 1242'.

Our family lived variously at 33 N.W.8
73 N.W.8, 1 and 9 N.W.11
And gloriously, for a while, in a country cottage
National Grid reference TQ 7024 (correct to one kilometre)

I passed my schooldays without much sense of identity
(Post Office Savings Book 1990 A)
In Lower and Upper 4ths and 5ths and 6ths
(Locker No 23)
Was 12th man in the 1st Eleven and rowed 7 in the Eight.

September 3rd, 1939 I was identified BIA 1526045;
1941-A. B. RNVR Red Division Starboard Watch
PJX 276123

And here I am
Stamped sealed and delivered

Passport No. 77922 F.O. London 23/2/66
National Insurance ZB 88 81 17B
Driving Licence 5Z/ 107206
Telephone Number 01 -983- Double 747
To my doctor: BIA 1526045
To my Banker: 37705873
To my Union: B 16776

V.A.T. 223 3859 66 . . .

And TIME LIFE International (Amsterdam) Inc. addresses me as
Mr Flanders 581 101 L03 FLAN-063 M 992 but refers to me in
private as 400000008400 1 00183N 06 S 29.

My days are numbered."

---- Michael Flanders (1/3/1922 - 14/4/1975)
 
C

CBFalconer

spl said:
I have an array of objects. I want to sort the array of a
given numeric value that exists in each of the objects. Suppose the
array of objects is called student and the numeric value I want to
sort the array based on the member is called "rollno".
ex:
struct student{
int rollno;
int name;
int status;
};
student obj[10]
here, I have to sort obj of 10objects, based on rollno.

To start with, there is not such type as 'student'. There is a
type 'struct student'. Unless you are using C++, in which case
this is the wrong newsgroup.

Your first project is to decide whether one object is greater,
equal, or less than another, based on the decisions you outlined.
So will need a routine like the following:

int cmpstudent(const void *leftstu, const void *rightstu) {
const struct student *left = leftstu;
const struct student *right = rightstu;

return (leftstu->rollno > rightstu->rollno)
-(leftstu->rollno < rightstu->rollno);
}

The use of void* allows use with such routines as qsort etc. Note
the absence of casts. Casts are usually errors. Note that
cmpstudent needs to know what it is comparing, but the code that
passes the void* pointers doesn't.
 
P

pete

Malcolm said:
spl said:
I have an array of objects. I want to sort the array of a
given numeric value that exists in each of the objects. Suppose the
array of objects is called student and the numeric value I want to
sort the array based on the member is called "rollno".
ex:
struct student{
int rollno;
int name;
int status;
};
student obj[10]
here, I have to sort obj of 10objects, based on rollno.

Please give your suggestion for any fast method?

int compfunct(const void *e1, const void *e2)
{
const student *s1 = e1;
const student *s2 = e2;

return s1->rollno - s2->rollno;
}

in main body of code:

qsort(obj, 10, sizeof(student), compfunct);

That's undefined if s1->rollno equals INT_MAX and s2->rollno equals -1,
and what you wrote
is also undefined for about a zillion other scenarios.

int compfunct(const void *e1, const void *e2)
{
const student *s1 = e1;
const student *s2 = e2;

return s2->rollno > s1->rollno ? -1 : s2->rollno != s1->rollno;
}
 

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
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top