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:
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?
 
T

Triple-DES

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.
 
U

utab

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
 
E

Eric Pruneau

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

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top