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. C / C++ / MFC
  4. c++ list sorting

c++ list sorting

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++dockeralgorithms
2 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.
  • M Offline
    M Offline
    mehmetned
    wrote on last edited by
    #1

    I have a class A and it has x,y and z integer attributes I have created a list sequence container which has a type A list alist I want to sort the elements in this list according to y value. How can i do that? alist.sort() only sorts the elements according to the first attribute defined in the constructor.

    S 1 Reply Last reply
    0
    • M mehmetned

      I have a class A and it has x,y and z integer attributes I have created a list sequence container which has a type A list alist I want to sort the elements in this list according to y value. How can i do that? alist.sort() only sorts the elements according to the first attribute defined in the constructor.

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      You have a number of options. The first is to define an operator < for your class:

      bool operator<(const A &lhs, const A &rhs)
      {
      return lhs.y < rhs.y;
      }

      You would make it a friend of class A if it needs access to private members of the class. Another option is to define a predicate function and pass it to the std::list<...>::sort function. This option would enable you to have multiple sort orders. Example:

      class A
      {
      // ...Stuff missing...
      public:
      static bool SortByYPred(const A &lhs, const A &rhs)
      {
      return lhs.y < rhs.y;
      }
      // ...Stuff missing...
      };

      Now call std::list<...>::sort like this:

      alist.sort(&A::SortByYPred);

      mehmetned wrote:

      alist.sort() only sorts the elements according to the first attribute defined in the constructor.

      The constructor has nothing to do with sort order.

      Steve

      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