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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:58:52+00:00 2026-05-27T01:58:52+00:00

I have this code: QVector<LogEvent *> currentItems; //add a bunch of LogEvent objects to

  • 0

I have this code:

QVector<LogEvent *> currentItems;
//add a bunch of LogEvent objects to currentItems
qSort(currentItems.begin(), currentItems.end());

This is my LogEvent class:

LogEvent.h:

//LogEvent.h
class LogEvent : public QTreeWidgetItem {

public:
    LogEvent();
    LogEvent(QDateTime, LogEvent *parent = 0);
    ~LogEvent();

    bool operator<(const LogEvent *);
    bool operator>(const LogEvent *);
    bool operator<=(const LogEvent *);
    bool operator>=(const LogEvent *);
    bool operator==(const LogEvent *);

private:

    QDateTime timestamp;
};

LogEvent.cpp:

//LogEvent.cpp
LogEvent::LogEvent()
{

}

LogEvent::LogEvent(QDateTime timestamp, LogEvent *parent)
    : QTreeWidgetItem(parent)
{
    this->timestamp = timestamp;
}

bool LogEvent::operator<(const LogEvent * event) {
    return (this->timestamp < event->timestamp);
}

bool LogEvent::operator>(const LogEvent * event) {
    return (this->timestamp > event->timestamp);
}

bool LogEvent::operator<=(const LogEvent * event) {
    return (this->timestamp <= event->timestamp);
}

bool LogEvent::operator>=(const LogEvent * event) {
    return (this->timestamp >= event->timestamp);
}

bool LogEvent::operator==(const LogEvent * event) {
    return (this->timestamp == event->timestamp);
}

After I do my sort, the LogEvent objects in currentItems are not sorted correctly. I am pretty sure my operator overloading is working.

When I do stuff like this:

std::cout << currentItems[0]<=currentItems[1]?"T":"F";

it will output the right value.

So what am I doing wrong and how do I correct it?

  • 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-05-27T01:58:53+00:00Added an answer on May 27, 2026 at 1:58 am

    qSort is sorting the pointers, not the objects pointed to by those pointers. If you want to sort LogEvents with qSort, you will have to store them by value and not by reference (and also have comparison operators that take a reference, qSort won’t find your comparison-to-pointer functions), or pass a third argument with a function you define.

    It might bear explaining why this is so with examples.

    LogEvent event1, event2;
    LogEvent *eventptr1=&event1,*eventptr2=&event2;
    event1<event2; // Operator not defined in your code
    event1<eventptr2; // This will call the operator you have defined
    eventptr1<eventptr2; // This will compare the pointers themselves, not the LogEvents. The pointers are not dereferenced here.
    

    ETA: In the interest of having a single complete answer to accept, I’m going to rip off some good bits from the other answers here.

    First, define a standard syntax less than operator:

    class LogEvent : public QTreeWidgetItem {
    
    public:
      // ...
      bool operator<(const LogEvent *); // Non-standard, possibly reasonable for use in your own code.
      bool operator<(const LogEvent &); // Standard, will be used by most template algorithms.
      // ...
    }
    

    LogEvent.cpp

    bool LogEvent::operator<(const LogEvent &event) {return timestamp<event.timestamp;}
    

    Once this is done, you can use this template dereference-and-compare from leemes’ answer:

    template<class T>
    bool dereferencedLessThan(T * o1, T * o2) {
        return *o1 < *o2;
    }
    

    to sort your list like so:

    QVector<LogEvent *> currentItems;
    //add a bunch of LogEvent objects to currentItems
    qSort(list.begin(), list.end(), dereferencedLessThan<LogEvent>);
    

    For completeness, it would be good form to define standard syntax comparison operators for all of your comparisons. Whether you keep your non-standard comparison operators is up to you.

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

Sidebar

Related Questions

I have this code in a single file : public class genIntro { public
I have this code but it doesn't work! public class Trial { public static
I have this code: <div class = content-dir-item> <p>Text input</p> <img src=./images/email.png class =
I have written some code that looks more or less like this: QVector<QRgb> colorTable(256);
I have this code for converting a byte[] to float[] . public float[] ConvertByteToFloat(byte[]
I have this code: public void replay() { long previous = DateTime.Now.Ticks; for (int
I have this code snippet where we get a collection from COM Dll public
I have this code in jQuery, that I want to reimplement with the prototype
I have this code: chars = #some list try: indx = chars.index(chars) except ValueError:
I have this code that performs an ajax call and loads the results into

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.