3 Replies - 137 Views - Last Post: 21 August 2012 - 07:25 AM Rate Topic: -----

#1 unknownGuy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • member icon

Reputation: 3057
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 TwoOfDiamonds  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • 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 .
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4888
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1