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

  • SEARCH
  • Home
  • 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 7734211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:11:35+00:00 2026-06-01T07:11:35+00:00

I have multiple data entries that contain the following information: id_number name1 date name2

  • 0

I have multiple data entries that contain the following information:
id_number
name1
date
name2

It is possible to put this into a struct like this:

struct entry {
  int id_number;
  string name1;
  int date;
  string name2;
}

In my data, I have many such entries and I would like to sort. First, I want to sort alphabetically based on name1, then sort by date. However, the sort by date is a subset of the alphabetical sort, e.g. if I have two entries with the same name1, I then want to order those entries by date. Furthermore, when I sort, I want the elements of the entry to remain together, so all four values go together.

My questions are the following:

1) What type of data structure should I use to hold this data so I can keep the set of four elements together when I sort any by any one of them?

2) What is the quickest way to do this sorting (in terms of amount of time to write the code). Ideally, I want to use something like the sort in algorithms.h since it is already built in.

3) Does STL have some built in data structure that can handle the double sorting I described efficiently?

  • 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-01T07:11:37+00:00Added an answer on June 1, 2026 at 7:11 am

    The struct you have is fine, except that you may want to add an overload of operator< to do comparison. Here I’m doing the “compare by name, then date” comparison:

    // Add this as a member function to `entry`.
    bool operator<(entry const &other) const {
        if (name1 < other.name1)
            return true;
        if (name1 > other.name1)
            return false;
    
        // otherwise name1 == other.name1
        // so we now fall through to use the next comparator.
    
        if (date < other.date)
            return true;
        return false;
    }
    

    [Edit: What’s required is called a “strict weak ordering”. If you want to get into detail about what the means, and what alternatives are possible, Dave Abrahams wrote quite a detailed post on C++ Next about it.

    In the case above, we start by comparing the name1 fields of the two. If a<b, then we immediately return true. Otherwise, we check for a>b, and if so we return false. At that point, we’ve eliminated a<b and a>b, so we’ve determined that a==b, in which case we test the dates — if a<b, we return true. Otherwise, we return false — either the dates are equal, or b>a, either of which means the test for a<b is false. If the sort needs to sort out (no pun intended) which of those is the case, it can call the function again with the arguments swapped. The names will still be equal, so it’ll still come down to the dates — if we get false, the dates are equal. If we get true on the swapped dates, then what started as the second date is actually greater. ]

    The operator< you define in the structure defines the order that will be used by default. When/if you want you can specify another order for the sorting to use:

    struct byid { 
        bool operator<(entry const &a, entry const &b) { 
            return a.id_number < b.id_number;
        }
    };
    
    std::vector<entry> entries;
    
    // sort by name, then date
    std::sort(entries.begin(), entries.end());
    
    // sort by ID
    std::sort(entries.begin(), entries.end(), byid());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of rows that contain duplicate entries because the data originates
I have a XML document with my data in it, multiple entries for the
I have multiple data items, each with a value V that has multiple tags
I have multiple text files with logged data like this: 6/23/09 17:00 0.443 6/23/09
I have multiple BIRT reports that obtains the data from the same jdbc data
I have multiple classes that will store similar data for different uses and I
I have data in a table that looks like the following sample data: varchar(20)
I need to get the TOTAL number of entries that contain duplicate data for
I have a pandas DataFrame that has multiple columns in it: Index: 239897 entries,
I have multiple textfiles with data as follows: 0.000000E+0 0.000000E+0 1.800000E-1 0.000000E+0 4.200000E-1 0.000000E+0

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.