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

sorting array of objects...

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?
Feb 19 '08 #1
4 3162
spl wrote:
[...]
here, I have to sort obj of 10objects, based on rollno.

Please give your suggestion for any fast method?
http://en.wikipedia.org/wiki/Insertion_sort
Feb 19 '08 #2

"spl" <sp**********@gmail.comwrote in message
news:e2**********************************@p25g2000 hsf.googlegroups.com...
>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);

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Feb 19 '08 #3
spl <sp**********@gmail.comwrites:
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?]

--
Ben.
Feb 19 '08 #4
Malcolm McLean wrote:
>
"spl" <sp**********@gmail.comwrote in message
news:e2**********************************@p25g2000 hsf.googlegroups.com...
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;
}

--
pete
Feb 19 '08 #5

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

Similar topics

1
by: Brian | last post by:
I have an array like this: $events = array( array( '2003-07-01', 'Event Title 1', '1' //ID Number (not unique) ), array( '2003-07-02',
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
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...
0
by: Alex Vinokur | last post by:
=================================== ------------- Sorting ------------- Comparative performance measurement =================================== Testsuite : Comparing Function Objects to...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
1
by: aredo3604gif | last post by:
On Sun, 10 Apr 2005 19:46:32 GMT, aredo3604gif@yahoo.com wrote: >The user can dynamically enter and change the rule connection between >objects. The rule is a "<" and so given two objects: >a <...
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?
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
5
by: DKC | last post by:
Hi, Using VB.NET. I have a datagrid having a strongly typed array of objects as its data source. The data from the array of objects is displayed by means of a table style, which is fine, but...
10
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to...
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...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.