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
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. stl sorting question

stl sorting question

Scheduled Pinned Locked Moved ATL / WTL / STL
questionc++graphicsalgorithmsdata-structures
3 Posts 2 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.
  • W Offline
    W Offline
    Warren Stevens
    wrote on last edited by
    #1

    How do I write a function that will help sort my array, by some dynamically specifiable criteria. For example: class Sortable{ int a; int b; int c; int d; int e; }; void SomeFunction() { std::vector< Sortable> items; // add some items int sortBy = 2; // variable saying I shoul sort by "a" (or "b" or "c", etc) std::stable_sort(items.begin(), items.end(), SortByAorBorC); } Is it possible to write a function "SortByAorBorC" that takes a paramter (the int sortBy)??? Right now I'm writing a separate function for each (e.g. "SortByA" "SortByB" and then using a switch statement - it works but it's not very elegant. I can't quite get it going... Many Thanks Warren

    L 1 Reply Last reply
    0
    • W Warren Stevens

      How do I write a function that will help sort my array, by some dynamically specifiable criteria. For example: class Sortable{ int a; int b; int c; int d; int e; }; void SomeFunction() { std::vector< Sortable> items; // add some items int sortBy = 2; // variable saying I shoul sort by "a" (or "b" or "c", etc) std::stable_sort(items.begin(), items.end(), SortByAorBorC); } Is it possible to write a function "SortByAorBorC" that takes a paramter (the int sortBy)??? Right now I'm writing a separate function for each (e.g. "SortByA" "SortByB" and then using a switch statement - it works but it's not very elegant. I can't quite get it going... Many Thanks Warren

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You want to use a function object. For example:

      class CMySortFn
      {
      private:
      int m_nSortBy;
      public:
      CMySortFn(int sortBy) : m_nSortBy(sortBy) { }
      // Called by std::sort when comparing items
      bool operator()(const Sortable& a, const Sortable& b) const
      {
      switch (m_nSortBy)
      {
      case 1:
      return a.a < b.a;
      case 2:
      return a.b < b.b;
      etc.
      }
      }
      };

      std::sort(items.begin(), items.end(), CMySortFn(2));

      You could also use a struct as the function object, if you want it even simpler. e.g.:

      struct mysortfn
      {
      int m_nSortBy;
      mysortfn(int sortBy) : m_nSortBy(sortBy) { }
      bool operator()(const Sortable& a, const Sortable& b) const
      {
      ...
      }
      };

      HTH. Have a good weekend.

      W 1 Reply Last reply
      0
      • L Lost User

        You want to use a function object. For example:

        class CMySortFn
        {
        private:
        int m_nSortBy;
        public:
        CMySortFn(int sortBy) : m_nSortBy(sortBy) { }
        // Called by std::sort when comparing items
        bool operator()(const Sortable& a, const Sortable& b) const
        {
        switch (m_nSortBy)
        {
        case 1:
        return a.a < b.a;
        case 2:
        return a.b < b.b;
        etc.
        }
        }
        };

        std::sort(items.begin(), items.end(), CMySortFn(2));

        You could also use a struct as the function object, if you want it even simpler. e.g.:

        struct mysortfn
        {
        int m_nSortBy;
        mysortfn(int sortBy) : m_nSortBy(sortBy) { }
        bool operator()(const Sortable& a, const Sortable& b) const
        {
        ...
        }
        };

        HTH. Have a good weekend.

        W Offline
        W Offline
        Warren Stevens
        wrote on last edited by
        #3

        Robert, Yes! That's exactly what I was looking for, I just didn't know the syntax. Thanks very much! :) Warren

        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