Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8165463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:32:31+00:00 2026-06-06T19:32:31+00:00

I have a key type: struct KeyT { uint32_t timestamp; // example! uint32_t a;

  • 0

I have a key type:

struct KeyT {
    uint32_t timestamp;

    // example!
    uint32_t a;
    uint32_t b;
    uint32_t c;
    uint32_t d;
    uint32_t e;
    // ...

    bool operator== (const KeyT& key) const
    {
        if(timestamp == key.timestamp && a == key.a && b == key.b && d == key.d && c == key.c && e == key.e)
            return true;
        return false;

    }
    bool operator< (const KeyT& key) const
    {
        if(timestamp < key.timestamp)
            return true;
        else if(timestamp == key.timestamp && a < key.a && b < key.b && c < key.c && d < key.d && e < key.e)
            return true;
        else if(timestamp == key.timestamp && a == key.a && b < key.b && c < key.c && d < key.d && e < key.e)
            return true;
        else if(timestamp == key.timestamp && a == key.a && b == key.b && c < key.c && d < key.d && e < key.e)
            return true;
        else if(timestamp == key.timestamp && a == key.a && b == key.b && c == key.c && d < key.d && e < key.e)
            return true;
        else if(timestamp == key.timestamp && a == key.a && b == key.b && c == key.c && d == key.d && e < key.e)
            return true;
        // ..
        return false;
    }
};

Now, I don’t really care about the sorting on member vars a, b, c, d, e, the only thing I want to ensure is that the map is sorted on timestamp. I also just realized that if I have two instances of KeyT one, two, where everything is the same except for “d”, then both one < two, and two < one would be false. The only way to fix that is to write comparisons for all possible combinations of all member variables.. I’m fairly certain I’m missing something obvious, so what’s the best solution in this case?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:32:34+00:00Added an answer on June 6, 2026 at 7:32 pm

    I think this does what you need:

    bool operator< (const KeyT& key) const
    {
        if(timestamp != key.timestamp) return timestamp < key.timestamp;
        else if ( a != key.a ) return a < key.a;
        else if ( b != key.b ) return b < key.b;
        else if ( c != key.c ) return c < key.c;
        else if ( d != key.d ) return d < key.d;
        else return e < key.e;
    }
    

    This is a sensible if ugly pattern to use whenever you have a prioritised list of variables you want to sort on within a comparable class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a structure of type: struct hashnode_s { struct hashnode_s *next; char *key;
I have a stl::map which key type is a custom struct. I want to
I have this struct type definition: typedef struct { char *key; long canTag; long
I have a Map that uses a Set for the key type, like this:
I have the following: type KEY = (IPv4, Integer) type TPSQ = TVar (PSQ.PSQ
I have declared the following type KEY = (IPv4, Integer) type TPSQ = TVar
I have 2 collections of objects. the objects have an object-type 'key' property (I
I have a table: +----------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key |
I have a table: +--------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key |
I have a problem putting data into an unordered_map using a struct as key:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.