sorting array of objects...

Discussion in 'C Programming' started by spl, Feb 19, 2008.

  1. spl

    spl Guest

    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?
     
    spl, Feb 19, 2008
    #1
    1. Advertising

  2. spl

    Pedro Graca Guest

    Pedro Graca, Feb 19, 2008
    #2
    1. Advertising

  3. "spl" <> wrote in message
    news:...
    >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
     
    Malcolm McLean, Feb 19, 2008
    #3
  4. spl <> writes:

    > 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.
     
    Ben Bacarisse, Feb 19, 2008
    #4
  5. Ben Bacarisse said:

    > spl <> writes:
    >

    <snip>

    >> struct student{
    >> int rollno;
    >> int name;
    >> int status;
    >> };
    >> student obj[10]

    >
    > This looks like C++ not C.

    <snip>
    >
    > [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)

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Feb 19, 2008
    #5
  6. spl

    CBFalconer Guest

    spl wrote:
    >
    > 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.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Feb 19, 2008
    #6
  7. spl

    pete Guest

    Malcolm McLean wrote:
    >
    > "spl" <> wrote in message
    > news:...
    > >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
     
    pete, Feb 19, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Meade
    Replies:
    4
    Views:
    30,422
    rahul891
    Nov 4, 2012
  2. TheVooDooChild

    Sorting an Array of String Objects

    TheVooDooChild, Mar 22, 2005, in forum: Java
    Replies:
    12
    Views:
    40,020
    Thomas G. Marshall
    Mar 25, 2005
  3. mutinymike

    sorting an array of objects

    mutinymike, Sep 11, 2007, in forum: Java
    Replies:
    0
    Views:
    346
    mutinymike
    Sep 11, 2007
  4. markspace
    Replies:
    1
    Views:
    390
    markspace
    Jun 25, 2009
  5. Roedy Green
    Replies:
    1
    Views:
    445
    Roedy Green
    Jun 25, 2009
Loading...

Share This Page