Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Sorting a vector

Sorting a vector

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphicsalgorithmshelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DrZOO
    wrote on last edited by
    #1

    Hello! I'm trying to use a vector, but it seems like I'm not that clever. How can I sort my vector by one of the Class attributes, in my case ctDate? typedef struct tagSCHEDULEBLOCK { CTime ctDate; CString csText; }SCHEDULEBLOCK; vector MyVector; Thanks for all your help! /DrZOO

    C 1 Reply Last reply
    0
    • D DrZOO

      Hello! I'm trying to use a vector, but it seems like I'm not that clever. How can I sort my vector by one of the Class attributes, in my case ctDate? typedef struct tagSCHEDULEBLOCK { CTime ctDate; CString csText; }SCHEDULEBLOCK; vector MyVector; Thanks for all your help! /DrZOO

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      you will have to write a comparison function that looks something like this:

      bool date_sort(const SCHEDULEBLOCK &a, const SCHEDULEBLOCK &b)
      {
      return (a.ctDate < b.ctDate);
      }

      then you can sort it like this:

      std::sort(vec.begin(), vec.end(), date_sort);

      -c


      Cheap oil. It's worth it!

      ISEffects - effects for images

      D 1 Reply Last reply
      0
      • C Chris Losinger

        you will have to write a comparison function that looks something like this:

        bool date_sort(const SCHEDULEBLOCK &a, const SCHEDULEBLOCK &b)
        {
        return (a.ctDate < b.ctDate);
        }

        then you can sort it like this:

        std::sort(vec.begin(), vec.end(), date_sort);

        -c


        Cheap oil. It's worth it!

        ISEffects - effects for images

        D Offline
        D Offline
        DrZOO
        wrote on last edited by
        #3

        It looks nice, but for some reason i get an error; d:\Program Files\Microsoft Visual Studio .NET\Vc7\include\algorithm(1856): error C2064: term does not evaluate to a function which point to row 2 and 3 in the code below (from algorithm) while (_First < _Pfirst && !_Pred(*(_Pfirst - 1), *_Pfirst) && !_Pred(*_Pfirst, *(_Pfirst - 1))) --_Pfirst; Can you please help me? Should I pass some parameters to date_sort? Thanks once again lifesaver ;) /DrZOO

        C 1 Reply Last reply
        0
        • D DrZOO

          It looks nice, but for some reason i get an error; d:\Program Files\Microsoft Visual Studio .NET\Vc7\include\algorithm(1856): error C2064: term does not evaluate to a function which point to row 2 and 3 in the code below (from algorithm) while (_First < _Pfirst && !_Pred(*(_Pfirst - 1), *_Pfirst) && !_Pred(*_Pfirst, *(_Pfirst - 1))) --_Pfirst; Can you please help me? Should I pass some parameters to date_sort? Thanks once again lifesaver ;) /DrZOO

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #4

          it compiles fine for me.

          #include "stdafx.h"
          #include #include typedef struct tagSCHEDULEBLOCK
          { CTime ctDate;
          CString csText;
          }SCHEDULEBLOCK;

          bool date_sort(const SCHEDULEBLOCK &a, const SCHEDULEBLOCK &b){ return (a.ctDate < b.ctDate);}

          void Test()
          {
          std::vector vec;
          std::sort(vec.begin(), vec.end(), date_sort);
          }

          -c


          Cheap oil. It's worth it!

          ISEffects - effects for images

          D 1 Reply Last reply
          0
          • C Chris Losinger

            it compiles fine for me.

            #include "stdafx.h"
            #include #include typedef struct tagSCHEDULEBLOCK
            { CTime ctDate;
            CString csText;
            }SCHEDULEBLOCK;

            bool date_sort(const SCHEDULEBLOCK &a, const SCHEDULEBLOCK &b){ return (a.ctDate < b.ctDate);}

            void Test()
            {
            std::vector vec;
            std::sort(vec.begin(), vec.end(), date_sort);
            }

            -c


            Cheap oil. It's worth it!

            ISEffects - effects for images

            D Offline
            D Offline
            DrZOO
            wrote on last edited by
            #5

            I think my problems are because my vector holds pointers (vector < SCHEDULEBLOCK* >). I read Paul Wolfensberger's article "Using the std::sort() Method" before but I don't quite get it. This code works...; struct MySort { bool operator()(const SCHEDULEBLOCK* a, const SCHEDULEBLOCK* b) { return (a->ctBlockDate < b->ctBlockDate); } }; ... but it doesn't look that nice. /DrZOO

            C 1 Reply Last reply
            0
            • D DrZOO

              I think my problems are because my vector holds pointers (vector < SCHEDULEBLOCK* >). I read Paul Wolfensberger's article "Using the std::sort() Method" before but I don't quite get it. This code works...; struct MySort { bool operator()(const SCHEDULEBLOCK* a, const SCHEDULEBLOCK* b) { return (a->ctBlockDate < b->ctBlockDate); } }; ... but it doesn't look that nice. /DrZOO

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #6

              this defines the comparison operation as a function of the object. the way i showed defined the comparison as a global function. either way is fine. DrZOO wrote: it doesn't look that nice it's STL - it shouldn't look nice! :) -c


              Cheap oil. It's worth it!

              ISEffects - effects for images

              P 1 Reply Last reply
              0
              • C Chris Losinger

                this defines the comparison operation as a function of the object. the way i showed defined the comparison as a global function. either way is fine. DrZOO wrote: it doesn't look that nice it's STL - it shouldn't look nice! :) -c


                Cheap oil. It's worth it!

                ISEffects - effects for images

                P Offline
                P Offline
                peterchen
                wrote on last edited by
                #7

                yup! :)


                The earth is not dying. It is being killed.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups