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 data using std::map or std::multimap

Sorting data using std::map or std::multimap

Scheduled Pinned Locked Moved C / C++ / MFC
sharepointadobealgorithmsbusinessquestion
5 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.
  • C Offline
    C Offline
    cmacgowan
    wrote on last edited by
    #1

    Hi ... Happy New Year. Hope everyone is ok. I have not been here for a while. I have some contact lens data I'm trying to sort. I have some sort rules are static (hard-coded) followed by other rules form 'normal' rules Here are the requirements: 1. First sort by Company and Product is a static order (hard-coded, non alphbetic) Sample o Company ProductName -------------------------------------------------------------------- 1. Ciba 1. Focus, 2. AirOptics, 3. Dailies, 4. then alphabetical. 2. Bausch & Lomb 1. PureVision, 2. LiquidEye, 3. Softlens then alphabetical. 2. Followed by Package size decending (80, 30 ...) 3. Followed by Base Curve decending (8.6, 8,0 ...) 4. Followed by Sphere decending (-0.5, 0.0, 1.0 ...) In the end the result might look like the following Sample data CO PR PS BC Sp ------------------------------- Ciba Focus 10 8.6 0.00 Ciba Focus 20 8.6 -0.25 Ciba Focus 20 8.6 -0.10 Ciba Focus 20 8.6 +0.25 Ciba Air 10 8.6 -0.25 Ciba Air 10 8.6 -0.10 Ciba Air 20 8.1 -0.25 Ciba Air 20 8.1 -0.10 Ciba Air 20 8.1 0.00 Ciba Air 20 8.6 +0.25 Ciba Air 20 8.6 +0.40 The data is contained in a struct with other data. The stuct is used in a map with a key. I am building a key to sort for the static requirments such as below. The key contains a prefix at the front of the key to force the static sort (non alphabetical), followed by a suffix to make the key unique. Sample key: "1_1_Ciba_Focus Monthlys_4" My plan to sort the other requirments is to take each of the other requirments (PackageSize, Base Curve and Sphere) and sort them against the previous element with the key. Since I will be sorting the following agaist the Package Size (10, 20 ...) - maybe I should be using the multimap, as it might be easter to sort this stuff without having the unique ID at the end of key ?? With the key of "1_1_Ciba Focus" I would sort on the PackSize and then set the key to produce the correct sort start: (std::multimap) key: 1_1_Ciba Focus" Sample data CO PR Key ------------------------------- Ciba Focus 1_1_Ciba Focus Ciba Focus 1_1_Ciba Focus Ciba Focus 1_1_Ciba Focus Ciba Focus 1_1_Ciba Focus Sample data CO PR PS Key ------------------------------- Ciba Focus 10 1_1_1_Ciba Focus Ciba Focus 20 1_1_2_Ciba Focus Ciba Focus 20 1_1_2_Ciba Focu

    A A 2 Replies Last reply
    0
    • C cmacgowan

      Hi ... Happy New Year. Hope everyone is ok. I have not been here for a while. I have some contact lens data I'm trying to sort. I have some sort rules are static (hard-coded) followed by other rules form 'normal' rules Here are the requirements: 1. First sort by Company and Product is a static order (hard-coded, non alphbetic) Sample o Company ProductName -------------------------------------------------------------------- 1. Ciba 1. Focus, 2. AirOptics, 3. Dailies, 4. then alphabetical. 2. Bausch & Lomb 1. PureVision, 2. LiquidEye, 3. Softlens then alphabetical. 2. Followed by Package size decending (80, 30 ...) 3. Followed by Base Curve decending (8.6, 8,0 ...) 4. Followed by Sphere decending (-0.5, 0.0, 1.0 ...) In the end the result might look like the following Sample data CO PR PS BC Sp ------------------------------- Ciba Focus 10 8.6 0.00 Ciba Focus 20 8.6 -0.25 Ciba Focus 20 8.6 -0.10 Ciba Focus 20 8.6 +0.25 Ciba Air 10 8.6 -0.25 Ciba Air 10 8.6 -0.10 Ciba Air 20 8.1 -0.25 Ciba Air 20 8.1 -0.10 Ciba Air 20 8.1 0.00 Ciba Air 20 8.6 +0.25 Ciba Air 20 8.6 +0.40 The data is contained in a struct with other data. The stuct is used in a map with a key. I am building a key to sort for the static requirments such as below. The key contains a prefix at the front of the key to force the static sort (non alphabetical), followed by a suffix to make the key unique. Sample key: "1_1_Ciba_Focus Monthlys_4" My plan to sort the other requirments is to take each of the other requirments (PackageSize, Base Curve and Sphere) and sort them against the previous element with the key. Since I will be sorting the following agaist the Package Size (10, 20 ...) - maybe I should be using the multimap, as it might be easter to sort this stuff without having the unique ID at the end of key ?? With the key of "1_1_Ciba Focus" I would sort on the PackSize and then set the key to produce the correct sort start: (std::multimap) key: 1_1_Ciba Focus" Sample data CO PR Key ------------------------------- Ciba Focus 1_1_Ciba Focus Ciba Focus 1_1_Ciba Focus Ciba Focus 1_1_Ciba Focus Ciba Focus 1_1_Ciba Focus Sample data CO PR PS Key ------------------------------- Ciba Focus 10 1_1_1_Ciba Focus Ciba Focus 20 1_1_2_Ciba Focus Ciba Focus 20 1_1_2_Ciba Focu

      A Offline
      A Offline
      Andrew Brock
      wrote on last edited by
      #2

      Maps are sorted by the key, so you cant have them stored in the static order for 1. Depending on how often you are adding to the list and to where i would recomend either a std::vector or std::list for storing the data in the static order Your question is a little unclear, but if i'm reading it correctly you need something like this.

      cmacgowan wrote:

      4. Followed by Sphere decending (-0.5, 0.0, 1.0 ...)

      The order there is ascending

      #include
      #include
      #include
      #include

      //save putting std:: infront of everything
      using std::string;
      using std::map;
      using std::vector;

      //This struct stores the data that is required for sorting
      class ProductTypeKey {
      public:
      ProductTypeKey() { }
      ProductTypeKey(unsigned _nPackageSize, float _nBaseCurve, float _nSphere)
      : nPackageSize(_nPackageSize), nBaseCurve(_nBaseCurve), nSphere(_nSphere) { }

      public:
      	unsigned nPackageSize;
      	float nBaseCurve;
      	float nSphere;
      

      };

      //"The data is contained in a struct with other data.". This struct stores "[the] other data"
      typedef struct {
      //the other data you spoke of
      } ProductTypeData;

      struct MyItemCompare {
      bool operator()(const ProductTypeKey &mkLeft, const ProductTypeKey &mkRight) const {
      //Compare the package sizes, if they are not equal, then we are sorting by this key
      if (mkLeft.nPackageSize != mkRight.nPackageSize) {
      //greater than for decending order
      return mkLeft.nPackageSize > mkRight.nPackageSize;
      }
      //Compare the base curves, if they are not equal, then we are sorting by this key
      if (mkLeft.nBaseCurve != mkRight.nBaseCurve) {
      //greater than for decending order
      return mkLeft.nBaseCurve > mkRight.nBaseCurve;
      }
      //Compare the sphere, if they are not equal, then we are sorting by this key
      if (mkLeft.nSphere != mkRight.nSphere) {
      //less than for ascending order
      return mkLeft.nSphere < mkRight.nSphere;
      }
      //We will only make it here if the 2 products are identical from the data we are sorting on.
      return false;
      }
      };

      //This struct contains the information that is stored in static order
      class Product {
      public:
      typedef map TypesMap;

      public:
      	Product() { }
      	Product(const string &\_strCompany, const string &\_strProduct)
      		: strCompany(\_strCompany), strProduct(\_strProduct) { }
      	void AddType(const ProductTypeKey &key, const ProductTypeData &data){
      		colType
      
      C 1 Reply Last reply
      0
      • C cmacgowan

        Hi ... Happy New Year. Hope everyone is ok. I have not been here for a while. I have some contact lens data I'm trying to sort. I have some sort rules are static (hard-coded) followed by other rules form 'normal' rules Here are the requirements: 1. First sort by Company and Product is a static order (hard-coded, non alphbetic) Sample o Company ProductName -------------------------------------------------------------------- 1. Ciba 1. Focus, 2. AirOptics, 3. Dailies, 4. then alphabetical. 2. Bausch & Lomb 1. PureVision, 2. LiquidEye, 3. Softlens then alphabetical. 2. Followed by Package size decending (80, 30 ...) 3. Followed by Base Curve decending (8.6, 8,0 ...) 4. Followed by Sphere decending (-0.5, 0.0, 1.0 ...) In the end the result might look like the following Sample data CO PR PS BC Sp ------------------------------- Ciba Focus 10 8.6 0.00 Ciba Focus 20 8.6 -0.25 Ciba Focus 20 8.6 -0.10 Ciba Focus 20 8.6 +0.25 Ciba Air 10 8.6 -0.25 Ciba Air 10 8.6 -0.10 Ciba Air 20 8.1 -0.25 Ciba Air 20 8.1 -0.10 Ciba Air 20 8.1 0.00 Ciba Air 20 8.6 +0.25 Ciba Air 20 8.6 +0.40 The data is contained in a struct with other data. The stuct is used in a map with a key. I am building a key to sort for the static requirments such as below. The key contains a prefix at the front of the key to force the static sort (non alphabetical), followed by a suffix to make the key unique. Sample key: "1_1_Ciba_Focus Monthlys_4" My plan to sort the other requirments is to take each of the other requirments (PackageSize, Base Curve and Sphere) and sort them against the previous element with the key. Since I will be sorting the following agaist the Package Size (10, 20 ...) - maybe I should be using the multimap, as it might be easter to sort this stuff without having the unique ID at the end of key ?? With the key of "1_1_Ciba Focus" I would sort on the PackSize and then set the key to produce the correct sort start: (std::multimap) key: 1_1_Ciba Focus" Sample data CO PR Key ------------------------------- Ciba Focus 1_1_Ciba Focus Ciba Focus 1_1_Ciba Focus Ciba Focus 1_1_Ciba Focus Ciba Focus 1_1_Ciba Focus Sample data CO PR PS Key ------------------------------- Ciba Focus 10 1_1_1_Ciba Focus Ciba Focus 20 1_1_2_Ciba Focus Ciba Focus 20 1_1_2_Ciba Focu

        A Offline
        A Offline
        Aescleal
        wrote on last edited by
        #3

        I'm not 100% sure what you're driving at but it looks as if you only want to use a map so you can synthesise a key and then use that to control the sort. The traditional way (at least in C++) to force a sort order in a collection is to implement operator< for the key (in a map) or the value (in a vector) that you're storing. So if you've got a class with your contact lens data in try something like:

        //contact_lens_data.h

        class contact_lens_data
        {
        // All the funkiness that's fit to print about contact lenses

        friend bool operator< (const contact\_lens\_data &left, const contact\_lens\_data &right );
        

        };

        //contact_lens_data.cpp

        bool operator< (const contact_lens_data &left, const contact_lens_data &right )
        {
        // Put the comparison in here, if left should appear towards the front of the sort order return true
        }

        Then you can store them in any collection and get the collation order you want using std::sort or the inserter for std::map. Cheers, Ash

        C 1 Reply Last reply
        0
        • A Andrew Brock

          Maps are sorted by the key, so you cant have them stored in the static order for 1. Depending on how often you are adding to the list and to where i would recomend either a std::vector or std::list for storing the data in the static order Your question is a little unclear, but if i'm reading it correctly you need something like this.

          cmacgowan wrote:

          4. Followed by Sphere decending (-0.5, 0.0, 1.0 ...)

          The order there is ascending

          #include
          #include
          #include
          #include

          //save putting std:: infront of everything
          using std::string;
          using std::map;
          using std::vector;

          //This struct stores the data that is required for sorting
          class ProductTypeKey {
          public:
          ProductTypeKey() { }
          ProductTypeKey(unsigned _nPackageSize, float _nBaseCurve, float _nSphere)
          : nPackageSize(_nPackageSize), nBaseCurve(_nBaseCurve), nSphere(_nSphere) { }

          public:
          	unsigned nPackageSize;
          	float nBaseCurve;
          	float nSphere;
          

          };

          //"The data is contained in a struct with other data.". This struct stores "[the] other data"
          typedef struct {
          //the other data you spoke of
          } ProductTypeData;

          struct MyItemCompare {
          bool operator()(const ProductTypeKey &mkLeft, const ProductTypeKey &mkRight) const {
          //Compare the package sizes, if they are not equal, then we are sorting by this key
          if (mkLeft.nPackageSize != mkRight.nPackageSize) {
          //greater than for decending order
          return mkLeft.nPackageSize > mkRight.nPackageSize;
          }
          //Compare the base curves, if they are not equal, then we are sorting by this key
          if (mkLeft.nBaseCurve != mkRight.nBaseCurve) {
          //greater than for decending order
          return mkLeft.nBaseCurve > mkRight.nBaseCurve;
          }
          //Compare the sphere, if they are not equal, then we are sorting by this key
          if (mkLeft.nSphere != mkRight.nSphere) {
          //less than for ascending order
          return mkLeft.nSphere < mkRight.nSphere;
          }
          //We will only make it here if the 2 products are identical from the data we are sorting on.
          return false;
          }
          };

          //This struct contains the information that is stored in static order
          class Product {
          public:
          typedef map TypesMap;

          public:
          	Product() { }
          	Product(const string &\_strCompany, const string &\_strProduct)
          		: strCompany(\_strCompany), strProduct(\_strProduct) { }
          	void AddType(const ProductTypeKey &key, const ProductTypeData &data){
          		colType
          
          C Offline
          C Offline
          cmacgowan
          wrote on last edited by
          #4

          Hi Andrew Brock, Thanks for the response. I'll look at it and try it out. Looks good. I'll post a follow up. Thanks, Chris

          1 Reply Last reply
          0
          • A Aescleal

            I'm not 100% sure what you're driving at but it looks as if you only want to use a map so you can synthesise a key and then use that to control the sort. The traditional way (at least in C++) to force a sort order in a collection is to implement operator< for the key (in a map) or the value (in a vector) that you're storing. So if you've got a class with your contact lens data in try something like:

            //contact_lens_data.h

            class contact_lens_data
            {
            // All the funkiness that's fit to print about contact lenses

            friend bool operator< (const contact\_lens\_data &left, const contact\_lens\_data &right );
            

            };

            //contact_lens_data.cpp

            bool operator< (const contact_lens_data &left, const contact_lens_data &right )
            {
            // Put the comparison in here, if left should appear towards the front of the sort order return true
            }

            Then you can store them in any collection and get the collation order you want using std::sort or the inserter for std::map. Cheers, Ash

            C Offline
            C Offline
            cmacgowan
            wrote on last edited by
            #5

            Thanks for the info I'll check it out Chris

            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