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 90243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:50:34+00:00 2026-05-10T22:50:34+00:00

I have an input file that I want to sort based on timestamp which

  • 0

I have an input file that I want to sort based on timestamp which is a substring of each record. I want to store multiple attributes of the

The list is currently about 1000 records. But, I want it to be able to scale up a bit just in case.

When I did it with a Linked List by searching the entire list for insertion it took about 20 seconds. Now, just filling up a vector and outputting to file is taking 4 seconds (does that sound too long)?

I would like to use merge sort or quick sort (merge sort appears to be a little easier to me). The trouble that I’m running into is that I don’t see many examples of implementing these sorts using objects rather than primitive data types.

I could use either a vector or Linked list. The feedback that I’ve gotten from this site has been most helpful so far. I’m hoping that someone can sprinkle on the magic pixie dust to make this easier on me 🙂

Any links or examples on the easiest way to do this with pretty decent performance would be most appreciated. I’m getting stuck on how to implement these sorts with objects because I’m newbie at C++ 🙂

Here’s what my new code looks like (no sorting yet):

class CFileInfo   {       public:       std::string m_PackLine;       std::string m_FileDateTime;       int m_NumDownloads;   };   void main()   {       CFileInfo packInfo;       vector<CFileInfo> unsortedFiles;       vector<CFileInfo>::iterator Iter;       packInfo.m_PackLine = 'Sample Line 1';       packInfo.m_FileDateTime = '06/22/2008 04:34';       packInfo.m_NumDownloads = 0;       unsortedFiles.push_back(packInfo);       packInfo.m_PackLine = 'Sample Line 2';       packInfo.m_FileDateTime = '12/05/2007 14:54';       packInfo.m_NumDownloads = 1;       unsortedFiles.push_back(packInfo);       for (Iter = unsortedFiles.begin(); Iter != unsortedFiles.end(); ++Iter )        {           cout << ' ' << (*Iter).m_PackLine;       }   }   
  • 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. 2026-05-10T22:50:35+00:00Added an answer on May 10, 2026 at 10:50 pm

    Sorting a linked-list will inherently be either O(N^2) or involve external random-access storage.

    Vectors have random access storage. So do arrays. Sorting can be O(NlogN).

    At 1000 elements you will begin to see a difference between O(N^2) and O(NlogN). At 1,000,000 elements you’ll definitely notice the difference!

    It is possible under very special situations to get O(N) sorting. (For example: Sorting a deck of playing cards. We can create a function(card) that maps each card to its sorted position.)

    But in general, O(NlogN) is as good as it gets. So you might as well use STL’s sort()!
    Just add #include <algorithms>


    All you’ll need to add is an operator<(). Or a sort functor.

    But one suggestion: For god’s sake man, if you are going to sort on a date, either encode it as a long int representing seconds-since-epoch (mktime?), or at the very least use a ‘year/month/day-hour:minute:second.fraction’ format. (And MAKE SURE everything is 2 (or 4) digits with leading zeros!) Comparing ‘6/22/2008-4:34′ and ’12/5/2007-14:54’ will require parsing! Comparing ‘2008/06/22-04:34’ with ‘2007/12/05-14:54’ is much easier. (Though still much less efficient than comparing two integers!)


    Rich wrote: the other answers seem to get into syntax more which is what I’m really lacking.

    Ok. With basic a ‘int’ type we have:

    #define PRINT(DATA,N) for(int i=0; i<N; i++) { cout << (i>0?', ':'') << DATA[i]; } cout << endl;  int main()   {     // Creating and Sorting a stack-based array.   int d [10] = { 1, 4, 0, 2, 8, 6, 3, 5, 9, 7 };   PRINT(d,10);   sort( d, d+10 );   PRINT(d,10);    cout << endl;      // Creating a vector.   int eData [10] = { 1, 4, 0, 2, 8, 6, 3, 5, 9, 7 };   vector<int> e;   for(int i=0; i<10; i++ )     e.push_back( eData[i] );      // Sorting a vector.   PRINT(e,10);   sort(e.begin(), e.end());   PRINT(e,10); } 

    With your own type we have:

    class Data {   public:     string m_PackLine;     string m_FileDateTime;     int    m_NumberDownloads;      /* Lets simplify creating Data elements down below. */   Data( const string & thePackLine  = '',         const string & theDateTime  = '',         int            theDownloads = 0 )       : m_PackLine        ( thePackLine  ),         m_FileDateTime    ( theDateTime  ),         m_NumberDownloads ( theDownloads )     { }      /* Can't use constructor with arrays */   void set( const string & thePackLine,             const string & theDateTime,             int            theDownloads = 0 )     {       m_PackLine        = thePackLine;       m_FileDateTime    = theDateTime;       m_NumberDownloads = theDownloads;     }      /* Lets simplify printing out down below. */    ostream & operator<<( ostream & theOstream ) const     {       theOstream << 'PackLine=\'' << m_PackLine                  << '\'   fileDateTime=\'' << m_FileDateTime                  << '\'   downloads=' << m_NumberDownloads;       return theOstream;     }       /*      * This is IT!  All you need to add to use sort()!      *  Note:  Sort is just on m_FileDateTime.  Everything else is superfluous.      *  Note:  Assumes 'YEAR/MONTH/DAY HOUR:MINUTE' format.      */   bool operator< ( const Data & theOtherData ) const     { return m_FileDateTime < theOtherData.m_FileDateTime; }  };      /* Rest of simplifying printing out down below. */  ostream & operator<<( ostream & theOstream, const Data & theData )   { return theData.operator<<( theOstream ); }       /* Printing out data set. */ #define PRINT(DATA,N) for(int i=0; i<N; i++) { cout << '[' << i << ']  ' << DATA[i] << endl; }  cout << endl;  int main() {       // Creating a stack-based array.   Data d [10];   d[0].set( 'Line 1', '2008/01/01 04:34', 1 );   d[1].set( 'Line 4', '2008/01/04 04:34', 4 );   d[2].set( 'Line 0', '2008/01/00 04:34', 0 );   d[3].set( 'Line 2', '2008/01/02 04:34', 2 );   d[4].set( 'Line 8', '2008/01/08 04:34', 8 );   d[5].set( 'Line 6', '2008/01/06 04:34', 6 );   d[6].set( 'Line 3', '2008/01/03 04:34', 3 );   d[7].set( 'Line 5', '2008/01/05 04:34', 5 );   d[8].set( 'Line 9', '2008/01/09 04:34', 9 );   d[9].set( 'Line 7', '2008/01/07 04:34', 7 );      // Sorting a stack-based array.   PRINT(d,10);   sort( d, d+10 );   PRINT(d,10);    cout << endl;      // Creating a vector.   vector<Data> e;   e.push_back( Data( 'Line 1', '2008/01/01 04:34', 1 ) );   e.push_back( Data( 'Line 4', '2008/01/04 04:34', 4 ) );   e.push_back( Data( 'Line 0', '2008/01/00 04:34', 0 ) );   e.push_back( Data( 'Line 2', '2008/01/02 04:34', 2 ) );   e.push_back( Data( 'Line 8', '2008/01/08 04:34', 8 ) );   e.push_back( Data( 'Line 6', '2008/01/06 04:34', 6 ) );   e.push_back( Data( 'Line 3', '2008/01/03 04:34', 3 ) );   e.push_back( Data( 'Line 5', '2008/01/05 04:34', 5 ) );   e.push_back( Data( 'Line 9', '2008/01/09 04:34', 9 ) );   e.push_back( Data( 'Line 7', '2008/01/07 04:34', 7 ) );      // Sorting a vector.   PRINT(e,10);   sort(e.begin(), e.end());   PRINT(e,10); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 138k
  • Answers 138k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You're looking for Core Data, which is part of the… May 12, 2026 at 7:31 am
  • Editorial Team
    Editorial Team added an answer See the docs: What is PHP's mysqli Extension? The mysqli… May 12, 2026 at 7:31 am
  • Editorial Team
    Editorial Team added an answer One option is to have one object instance per command/request;… May 12, 2026 at 7:31 am

Related Questions

Theoretically, the end user should never see internal errors. But in practice, theory and
There is a question I have been wondering about for ages and I was
I was hacking away the source code for plink to make it compatible with
Has anyone done something like this? How? I'm just starting a project that will

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.