how to sort a vector of object

Nov 25, 2008 at 1:52am
hi, Ive a vector of TaxPayer objects, instances of TaxPayer have an instance variable of type float call tax, I want to sort such that it lists the taxpayers in increasing order of how much tax they pay.

i've been trying stuff like
sort (taxpayers.begin(), taxpayers.end());
however this throws up all sorts of errors, as i think this is only applicable to vectors of primitive datatypes....

i.e.
1
2
3
int myints[] = {32,71,12,45,26,80,53,33};
      vector<int> myvector (myints, myints+8); 
      sort (myvector.begin(), myvector.begin()+4);  

so hopefully someone could tell me how to sort a vector of objects based on a member variable...

p.s.

im using pointers when populating my vector dont know if dat makes a dif ie
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
while (taxStream >> current) {
		//the tax payers wage
		float wage;
		//the tax payers credit
		float credit;

	    taxStream >> wage;
	    taxStream >> credit;

		TaxPayer *t = new TaxPayer(current, wage, credit);
		t->taxAmount();
		t->netPay();
		//And add it to the vector
		taxpayers.push_back(*t);
		pop.add(*t);
		pop.setTax(t->amountDue());
		delete t;

	}//end while() 

thanks
Last edited on Nov 25, 2008 at 2:51am
Nov 25, 2008 at 2:05am
The difference is that int already has an implicit sorting algorithm. You need to provide that to std::sort() in order to... sort. You'll probably want to sort based on some kind of ID or by name.
Nov 25, 2008 at 2:18am
yeah i already know dat lol, but as asked how do u do it... i want to sort on a member variable of the TaxPayer object called tax of type float... can someone please help lol...
Nov 25, 2008 at 2:35am
Something like this?
1
2
3
4
5
6
7
struct taxpayersCmp{
	bool operator()(const taxpayer &a,const taxpayer &b) const{
		return a.Float<b.Float;
	}
} taxpayersCmp_;
//...
std::sort(taxpayers.begin(),taxpayers.end(),taxpayersCmp_);

You should have looked closer at the example. There was an example class there, too.
Nov 25, 2008 at 3:17am
thanks mate, but example doesnt work for me, think its cus Im using pointers and TaxPayers is a class not a struct....
Nov 25, 2008 at 4:09am
Well, then just change the parameters from references to pointers, and write accessor methods for the member you want to compare.

Duh!
Nov 25, 2008 at 4:23am
well sorry, but im only writting in c++ few weeks not used to pointers in java so really haven't a clue how to do that, but would appriciate dearly if u could show me how to....
Nov 25, 2008 at 4:36am
Just replace & with * and . with ->
Simple, isn't it?
Nov 25, 2008 at 4:47am
lol im beginning to think its u thats simple my friend....

Im using pointers as stated many times above and not references to memory addresses so im off course already using * and ->.... now if you can only provide vauge unhelpful asides, I rather wait until 2maro when, perhaps I could get some help of someone more european (ie less condescending and actually helpfull) and to the point n less of a know it all without solutions as many americans tend to be......

anyway i sorted by creating/recreating an insertionSort method myself but would of liked to learn how to sort a vector of pointers to objects in C++
Last edited on Nov 25, 2008 at 4:52am
Nov 25, 2008 at 5:30am
I meant replace in my example. Why would I tell you to change the references in your program to pointers if I already know you're using pointers?

I hardly think a complete working example (two if you consider that I also gave you the steps on how convert it to pointers which you could have followed had not spent your time misinterpreting my words) qualifies as a "vague unhelpful aside". I should have just posted the link to the std::sort() reference. Would that have been more helpful?

And I'm not American (not even close, really), but we don't take too kindly to this kind of faggotry, around here, either.
Nov 25, 2008 at 12:35pm
Post some code. We can be more helpful if we see the declaration of the vector and the class that it is going to contain.
Nov 25, 2008 at 10:40pm
lol found out meself what was wrong i needed a random access iterator.... thanks seymore...
Topic archived. No new replies allowed.