1

I have the overloaded operator in Animal like this

// A must be comparable to be used as keys
bool operator<(const Archivo &right) const
{
    return nombreArchivo < right.nombreArchivo;
}

and in my main I call

std::vector<Animal*> animalesConConcha;
// add some objects
std::sort(animalesConConcha.begin(), animalesConConcha.end());

std::cout<<"\n\n\nOrdered:\n";

for(it=animalesConConcha.begin(); it!=animalesConConcha.end(); it++)
{
    cout<<(*it)->nombre<<" | "<<(*it)->indice<<endl;
}

But the output is still unsorted.

pmr
  • 58,701
  • 10
  • 113
  • 156
SDV
  • 33
  • 1
  • 7
  • Make sure you add a language tag when asking questions in a specific language (I've added C++ for you). – crashmstr Jul 11 '14 at 15:18
  • 4
    Those are `Animal*`s, not `Animal` objects. `std::sort()` is therefore sorting them by their addresses. – David G Jul 11 '14 at 15:19

4 Answers4

1

Because you are storing a vector of pointers to Animal you are not giving chance for the operator<() of your class to work. The sort function uses the operator<() for the type of what is in the vector - in this case a pointer to Animal, not an instance of Animal.

Therefore the array is sorted based on operator<() for pointers which as 0x499602D2 says will result in the array of pointers being sorted in ascending address.

If you want it to work this way, either define a custom comparator or use vector<Animal> instead of vector<Animal*>

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
1

You are storing pointers in your vector, not objects. std::sort does not dereference the pointers but compares the actual pointer values. (Technically, this does not have a guaranteed behaviour, because < is used directly on the pointers.)

Solution 1: Store the objects directly in the vector:

vector<Animal> animalesConConcha;
sort(animalesConConcha.begin(), animalesConConcha.end());

for(it=animalesConConcha.begin(); it!=animalesConConcha.end(); it++)
{
    cout<< it->nombre<<" | "<< it->indice<<endl;
}

Solution 2: Specify a custom comparison functor for std::sort:

struct Comparison
{
    bool const operator()(Animal *lhs, Animal *rhs) const
   {
         return (*lhs) < (*rhs);
    }
};

sort(animalesConConcha.begin(), animalesConConcha.end(), Comparison());
Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
0

Take into account that you don't have a vector with Animals.

In your code, there is a vector of pointers to Animals.

Change

vector<Animal*>

for

vector<Animal>

And tell us if is working now.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
Ivan
  • 1
0

I'm assuming it's polymorphism you want to achieve which is the reason why you're using Animal*s instead of stack-allocated Animal objects. Since you are using pointers as the value type of the vector, std::sort()s use of operator<() resolves to the built-in overload which will simply compare the addresses of the pointers instead of your custom operator.

Conveniently, std::sort() takes a predicate as an optional third parameter. You can pass a callable entity to be invoked as the comparator:

std::sort(animalesConConcha.begin(), animalesConConcha.end(),
[] (Animal* lhs, Animal* rhs) {
    return lhs->nombreArchivo < rhs->nombreArchivo;
});

If lambda expressions are not supported by your compiler, you can use a function or a class instance with an overloaded operator() instead.

David G
  • 94,763
  • 41
  • 167
  • 253