Results 1 to 7 of 7

Thread: Vector Sort C++

  1. #1
    Senior Member crazyfool's Avatar
    Join Date
    Jan 2008
    Posts
    761
    Thanks
    77
    Thanked
    38 times in 38 posts
    • crazyfool's system
      • Motherboard:
      • Striker Extreme
      • CPU:
      • Q6600
      • Memory:
      • OCZ 6400 2 x 2GB
      • Storage:
      • Samsung Spinpoint 500GB
      • Graphics card(s):
      • BFG 8800 GT OC 512MB
      • PSU:
      • Coolermaster Real Power Pro 850w
      • Case:
      • Coolermaster CM-690
      • Operating System:
      • XP, Vista, Windows 7 & Ubuntu
      • Monitor(s):
      • Samsung 2232BW

    Vector Sort C++

    hey guys,
    I need a little help. I have a vector of objects, each object has a name and an age variable. I want to be able to sort the vector by each objects age. Google brings up alot of results but I don't really understand most of them. This is what I've got so far. (might be some syntax mistakes, not a copy and paste job)

    Class people{ string name, int age..... }

    vector<people> vec;

    bool SortMethod (const people& person1, const people& person2) const
    { return person1.getage() < person2.getage(); }

    void Sort()
    {
    (vec.begin(), vec.end(), SortMethod);
    }


    Now according to all the threads I've read this is how to do it but it doesn't seem to do anything. Little help please. Thanks.

  2. #2
    HEXUS webmaster Steve's Avatar
    Join Date
    Nov 2003
    Posts
    14,283
    Thanks
    293
    Thanked
    841 times in 476 posts

    Re: Vector Sort C++

    How big is the vector going to be? My mind's not in a place where it can deal with C++ right now, but by the looks of it that's going to be Bubble Sort - which be slow! Still, not a problem if it's not many entries.
    PHP Code:
    $s = new signature();
    $s->sarcasm()->intellect()->font('Courier New')->display(); 

  3. #3
    Senior Member crazyfool's Avatar
    Join Date
    Jan 2008
    Posts
    761
    Thanks
    77
    Thanked
    38 times in 38 posts
    • crazyfool's system
      • Motherboard:
      • Striker Extreme
      • CPU:
      • Q6600
      • Memory:
      • OCZ 6400 2 x 2GB
      • Storage:
      • Samsung Spinpoint 500GB
      • Graphics card(s):
      • BFG 8800 GT OC 512MB
      • PSU:
      • Coolermaster Real Power Pro 850w
      • Case:
      • Coolermaster CM-690
      • Operating System:
      • XP, Vista, Windows 7 & Ubuntu
      • Monitor(s):
      • Samsung 2232BW

    Re: Vector Sort C++

    About 10 objects in the vector

  4. #4
    Ah, Mrs. Peel! mike_w's Avatar
    Join Date
    Oct 2003
    Location
    Hertfordshire, England
    Posts
    3,326
    Thanks
    3
    Thanked
    9 times in 7 posts

    Re: Vector Sort C++

    Firstly, define a comparator for sorting (as you have done with SortMethod). As well as defining a function, you can also define an object to do the same job:

    Code:
    struct AgeComparator {
        bool operator() (const People & first, const People & second) {
            return first.getage() < second.getage();
        }
    } ageComparator;
    Then, where you want to sort the vector:

    Code:
        sort(vec.begin(), vec.end(), ageComparator);
    "Well, there was your Uncle Tiberius who died wrapped in cabbage leaves but we assumed that was a freak accident."

  5. #5
    HEXUS.social member finlay666's Avatar
    Join Date
    Aug 2006
    Location
    Newcastle
    Posts
    8,546
    Thanks
    297
    Thanked
    894 times in 535 posts
    • finlay666's system
      • CPU:
      • 3570k
      • Memory:
      • 16gb
      • Graphics card(s):
      • 6950 2gb
      • Case:
      • Fractal R3
      • Operating System:
      • Windows 8
      • Monitor(s):
      • U2713HM and V222H
      • Internet:
      • cable

    Re: Vector Sort C++

    Not using a list instead?

    http://www.cplusplus.com/reference/stl/list/sort/

    Not sure why you are using a vector tbh
    H3XU5 Social FAQ
    Quote Originally Posted by tiggerai View Post
    I do like a bit of hot crumpet

  6. #6
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: Vector Sort C++

    Actually a vector would be better for things like quick sort, because of the random access ability.

    a List is not designed to allow simple random access.
    throw new ArgumentException (String, String, Exception)

  7. #7
    Senior Member crazyfool's Avatar
    Join Date
    Jan 2008
    Posts
    761
    Thanks
    77
    Thanked
    38 times in 38 posts
    • crazyfool's system
      • Motherboard:
      • Striker Extreme
      • CPU:
      • Q6600
      • Memory:
      • OCZ 6400 2 x 2GB
      • Storage:
      • Samsung Spinpoint 500GB
      • Graphics card(s):
      • BFG 8800 GT OC 512MB
      • PSU:
      • Coolermaster Real Power Pro 850w
      • Case:
      • Coolermaster CM-690
      • Operating System:
      • XP, Vista, Windows 7 & Ubuntu
      • Monitor(s):
      • Samsung 2232BW

    Re: Vector Sort C++

    Tbh I only used vectors because that is what I was most familiar with, they do the job for what I need them for anyways
    Haven't been able to try out mike_w's code yet but I'll give it a go later on today
    Thanks for all the replies anyways guys

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •