# Putting a vector of Objects in order

Page 1 of 1

## 3 Replies - 137 Views - Last Post: 21 August 2012 - 07:25 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=289592&amp;s=0f3580b18ec240583e1a29295adff70b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 unknownGuy

Reputation: 0
• Posts: 1
• Joined: 21-August 12

# Putting a vector of Objects in order

Posted 21 August 2012 - 06:34 AM

I was wondering how I could possibly put the vector "guy" in order by "done()". Here is what I have so far:

```double done;
vector<people> guy;
vector<people> newGuy;
for ( int i = 0; i < id; i++ )
{
done = guy[i].done();
int smallest = i;
for( int j = i + 1; j < id; j++ ){
if( done > guy[j].done() )
{
done = guy[j].done();
smallest = j;
}
}
newGuy.push_back( guy[smallest] );
}

```

This doesn't organize every part of the vector, and sometimes even copies the same guy into the newGuy. Any ideas?

Is This A Good Question/Topic? 0

## Replies To: Putting a vector of Objects in order

### #2 jimblumberg

Reputation: 3057
• Posts: 9,302
• Joined: 25-December 09

## Re: Putting a vector of Objects in order

Posted 21 August 2012 - 06:47 AM

If you can't use std::sort I suggest you research sorting. You are not even close to having a proper sort routine in the code you posted.

Jim

### #3 TwoOfDiamonds

Reputation: 38
• Posts: 200
• Joined: 27-July 12

## Re: Putting a vector of Objects in order

Posted 21 August 2012 - 07:13 AM

As jimblumberg mentioned , why don't you use std::sort , and as a general hint . Try to learn more about STL (standard template library), especially algorithms , maps and vectors .

Here you got STL Containers and STL algorithms.

Off Topic :

By the way , jim, congrats for your most recent award .

### #4 baavgai

• Dreaming Coder

Reputation: 4888
• Posts: 11,284
• Joined: 16-October 07

## Re: Putting a vector of Objects in order

Posted 21 August 2012 - 07:25 AM

Well, it looks like a basic selection sort, except you've missed the money shot. Sort do this sort, once you've found the smallest value, you change the list of values you're working in. You don't do that, you push the value into another list, but you really don't affect the list you started with.

You could make an array of indexes, sort that, and then use that to generate a new list.

You could do a simple insertion sort. Here, insertion is trivial, because vector has an insert function.

Or, just use sort.