sorting array of objects...

Discussion in 'C++' 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:
    class 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

    Triple-DES Guest

    On 19 Feb, 13:39, 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:
    > class 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?


    Make an operator< for student that returns whether one student's
    rollno is larger than the other, and call std::sort(obj, obj+10).
    Or make a free function or function object functionally equivalent to
    operator<, and pass it to std::sort.
    Triple-DES, Feb 19, 2008
    #2
    1. Advertising

  3. spl

    utab Guest

    On Feb 19, 1:39 pm, 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:
    > class 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?

    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>

    using namespace std;

    class student{

    public:
    // added c-tor
    student(int r,int n, int s):rollno(r),name(n),status(s) { }
    int getRollno() const { return rollno;}
    int getName() const { return name;}
    int getStatus() const { return status;}

    private:

    int rollno;
    int name;
    int status;
    };

    bool compare(const student& x, const student& y) {
    return x.getRollno() < y.getRollno();
    }

    int main(){
    vector<student> students;
    student s1(10,20,30);
    student s2(40,50,60);
    student s3(25,85,98);
    students.push_back(s1);
    students.push_back(s2);
    students.push_back(s3);

    sort(students.begin(),students.end(),compare);
    for(vector<student>::const_iterator iter=students.begin();
    iter!=students.end();++iter)
    std::cout << (*iter).getRollno() << '\t' << (*iter).getName() <<
    '\t' << (*iter).getStatus() << '\n';

    return 0;
    }

    Hope this helps a bit,

    Rgds,
    Here is a very primitive example with the compare that you can supply
    to the STL sort
    utab, Feb 19, 2008
    #3
  4. spl

    Eric Pruneau Guest

    "spl" <> a écrit dans le message de 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:
    > class 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?


    class student {
    int rollno;
    int name;
    int status;
    public: // operator< must be public, rollno needs not
    bool operator<(student s const&) const { return rollno < s.rollno; }
    }

    int main()
    {
    student obj[10]
    // Now I wonder how you populate obj since your members are private....
    // You probably want everything to be public...
    // Suppose obj is filled correctly as you want....
    std::sort(obj,obj+10);
    // now obj is sorted
    return 0;
    }

    Note that if you want to sort your objects based on anything else than
    rollno, you will have to supply a sorting function.

    -----
    Eric Pruneau
    Eric Pruneau, Feb 19, 2008
    #4
    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,369
    rahul891
    Nov 4, 2012
  2. TheVooDooChild

    Sorting an Array of String Objects

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

    sorting an array of objects

    mutinymike, Sep 11, 2007, in forum: Java
    Replies:
    0
    Views:
    326
    mutinymike
    Sep 11, 2007
  4. markspace
    Replies:
    1
    Views:
    370
    markspace
    Jun 25, 2009
  5. Roedy Green
    Replies:
    1
    Views:
    417
    Roedy Green
    Jun 25, 2009
Loading...

Share This Page