Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sorting array of objects...

Reply
Thread Tools

sorting array of objects...

 
 
spl
Guest
Posts: n/a
 
      02-19-2008
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?
 
Reply With Quote
 
 
 
 
Triple-DES
Guest
Posts: n/a
 
      02-19-2008
On 19 Feb, 13:39, spl <splender....@gmail.com> 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.
 
Reply With Quote
 
 
 
 
utab
Guest
Posts: n/a
 
      02-19-2008
On Feb 19, 1:39 pm, spl <splender....@gmail.com> 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,9;
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

 
Reply With Quote
 
Eric Pruneau
Guest
Posts: n/a
 
      02-19-2008

"spl" <> a écrit dans le message de news:
818778ec-863c-4e06-ad32-ec8a4d6d7457...oglegroups.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:
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting list vs sorting vector boltar2003@boltar.world C++ 2 07-06-2010 09:40 AM
Re: Array objects get changed when sorting the array Roedy Green Java 1 06-25-2009 08:25 PM
Re: Array objects get changed when sorting the array markspace Java 1 06-25-2009 06:22 PM
Sorting array against other array Andrew Poulos Javascript 4 01-23-2007 05:49 AM
Sorting through an array of an array Dominic Son Ruby 5 10-13-2006 11:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57