c++ sorting custom objects in vector

c++ sort vector of objects by attribute
how to sort a vector of class objects in c++
c++ sort vector of objects alphabetically
sort vector c++
c++ sort vector of objects lambda
c++ sort list of objects
vector of custom class c++
quick sort vector c++

Many topics on stackoverflow have tackled this subject, but I can't manage to get it right for my example. I have a class Event with the time that the event takes place. I want to sort these objects in a vector according to that time.

I first started implementing the operator<, but then the compiler gave the following error:

Error 1 error C2582: 'operator =' function is unavailable in 'Event' c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm 3009 1 sorting

So then I added the operator=

Below is the code I have used:

#include <algorithm>    // std::sort
#include <vector>       // std::vector

using namespace std;

class Event{
public:
     const double time;
     Event(double);
     bool operator<(const Event&);
     bool operator=(const Event&);
};

Event::Event(double time) :
time(time){

}

bool Event::operator<(const Event& rhs)
{
    return this->time < rhs.time;
}

bool Event::operator=(const Event& rhs)
{
    return this->time == rhs.time;
}

int main() {
    vector<Event> vector;
    Event e1 = Event(10);
    Event e2 = Event(5);
    Event e3 = Event(7);
    vector.push_back(e1);
    vector.push_back(e2);
    vector.push_back(e3);
    sort(vector.begin(), vector.end());
    return 0;
}

When I debug, I notice that my objects aren't sorted at all. They are in the order I added them. Below is an excerpt of the 'vector' variable:

[0] {time=10.000000000000000 }  Event
[1] {time=5.0000000000000000 }  Event
[2] {time=7.0000000000000000 }  Event

I have the following questions:

  1. Why aren't my events sorted in the vector when I call sort?
  2. Why does sort need operator= ?

The problem is that objects of your class Event cannot be assigned because of const double time member. Since the member is const it cannot be modified, so you cannot use sort on a container with Event objects, because sorting requires assignment.

Either remove const or rethink what you try to do. BTW you are confusing assignment operator (operator=, this is what sort requires) with equality operator (operator==`).

Sorting a vector of custom objects, Sorting a vector of custom objects · c++ stl sorting. How does one go about sorting a vector containing custom (i.e. user defined) objects. Probably  You can sort a vector of custom objects using the C++ STL function std::sort. The sort function has an overloaded form that takes as arguments first, last, comparator. The first and last are iterators to first and last elements of the container. The comparator is a predicate function that can be used to tell how to sort the container. example

To reorder the objects inside the vector the object has to be copyable (or moveable). You have to define an operator= (which is an assignment) that is of the form:

Event& operator=(const Event& other) 
{
    if(&other != this) { this->time = other.time; }
    return *this;
}

So your implementation of operator= is wrong. You are comparing the two objects but this is not what operator= (assignment!) is supposed to do. It has to assign one object (its content) to another object and return a reference to the object assigned to. Keep in mind that you should provide a copy constructor as well if you implement an assignment operator.

How to sort a vector of custom objects in C++, In this post, we will discuss how to sort a vector of custom objects in C++. The STL library provides std::sort algorithm that is defined in the header which can be​  How to sort a vector of custom objects in C++ In this post, we will discuss how to sort a vector of custom objects in C++. The STL library provides std::sort algorithm that is defined in the <algorithm> header which can be used to sort objects of any type.

This is the correct implementation of the custom assignment operator for the question:

Event& Event::operator=(const Event& other) 
{
    if(&other != this) {
        // As time is const, we modify it through a secondary reference
        // you can say it cheating with the compiler
        double &rTime = (double&) this->time;
        rTime = other.time;      // this modifies this->time variable
    }
    return *this;
}

Also, change the declaration of the assignment operator as well:

class Event{
public:
     Event& operator=(const Event&);
     // other declarations...
};

How to sort a vector of custom objects in C++98, This article shows how to sort a collection of custom Skyscraper objects using sort algorithm from the standard C++ library. You will only need basic knowledge of  Note: If you need to sort custom objects in C++11, read this article which shows how to accomplish the sorting with the help of a lambda expression. How to use the sort algorithm What we will try to do in this article is to sort the Skyscrapers by their height.

C++ : How to Sort a List of objects with custom Comparator or , C++ : How to Sort a List of objects with custom Comparator or lambda function discuss different ways to sort a std::list of user defined data types with custom C++ : How to compare two vectors | std::equal() & Comparators  sort () takes a third parameter that is used to specify the order in which elements are to be sorted. We can pass “greater ()” function to sort in descending order. This function does comparison in a way that puts greater element before. // C++ program to sort a vector in non-increasing. #include <bits/stdc++.h> using namespace std;

Sorting a vector in C++, How to use make utility to build C projects?` Program to create Custom Vector Class in C++ · fpclassify() method in C/C++ with Examples · boost is_pointer  Sorting a List of Objects with Custom Comparator & Lambda Function We can also sort a list of Player objects by name using a Lambda function instead of defining a seperate Function object like above. We can pass a lambda function in sort() as argument. this lambda function will compare two Player objects by name.

std::sort - sort, custom (2) This can either be a function pointer or a function object. std::cout #include <algorithm> // std::sort #include <vector> // std::vector bool myfunction  How to use lambda expression to sort a vector of custom objects (since C++11) In this article, we are going to sort a vector of user-defined objects, specifically Skyscrapers, with the help of a lambda expression introduced by the C++11. In the first variant of the problem, we are going to sort the objects according to their height.

Comments
  • operator= is the assignment operator. Perhaps you wanted operator==.
  • Your argument naming in the operator functions are misleading, as the argument is actually the right hand side of the expression.
  • possible duplicate of Non-static const member, can't use default assignment operator
  • Don't make const members in classes, make const classes instead.
  • @NeilKirk : or just make it private, with a public getter.
  • The class cannot be copied/assigned due to a const non-static member.
  • I did not recognize the const. I think without removing the constness of time there will be no way for doing an inplace sort.