How to order a list of objects?

1

I need help with this. I have a list in which I must store the information of the taxi drivers, in one of the functions I must print a report of taxi drivers who worked overtime, ordered in descending order. I have seen several examples on the internet but none has helped me, this is the code:

void ReporteHorasExtra() {
    for (list<Taxista*>::iterator it = taxistas.begin(); it != taxistas.end(); it++) {
        if ((*it)->horasExtras() > 0) {
            cout << "Nombre:";
            cout<<(*it)->getNombre();
            cout << ' ';
            cout<<(*it)->getApellido();
            cout << ' '<<"Sexo: ";
            if ((*it)->getSexo() == true) {
                cout << "Femenino" << ' ';
            }
            if ((*it)->getSexo() == false) {
                cout << "Masculino" << ' ';
            }
            cout << "Cantidad de horas extra: ";
            cout<<(*it)->horasExtras();
            cout << endl;
        }
    }
}

That's what I have, but I do not know how to order them.

Issue # 1

I have updated the function in the following way, maybe it is very bad and I am sorry for that, but I have not understood completely.

void ReporteHorasExtra() {
        for (list<Taxista>::iterator it = taxistas.begin(); it != taxistas.end(); it++) {
            vector<Taxista> horas(taxistas.size());
            copy(taxistas.begin(), taxistas.end(), horas.begin());

            sort(horas.begin(), horas.end(), [](auto &a, auto &b) { return a->horasExtras() > b->horasExtras(); });
            if ((it)->horasExtras() > 0) {
                cout << "Nombre:";
                cout<<(it)->getNombre();
                cout << ' ';
                cout<<(it)->getApellido();
                cout << ' '<<"Sexo: ";
                if ((it)->getSexo() == true) {
                    cout << "Femenino" << ' ';
                }
                if ((it)->getSexo() == false) {
                    cout << "Masculino" << ' ';
                }
                cout << "Cantidad de horas extra: ";
                cout<<(it)->horasExtras();
                cout << endl;
            }
        }

    }

This is what I have done, but I have obtained these errors.

    
asked by davidllerenav 28.11.2018 в 23:00
source

1 answer

1

As far as I can see, your problem is in the understanding of code; and it's normal because you're looking at the more complicated examples. What if I told you that sorting data is much easier than you think?:

// Empezamos copiando los taxistas en un vector
std::vector<Taxista*> horas(taxistas.size());
std::copy(taxistas.begin(), taxistas.end(), horas.begin());

// Ordenamos
std::sort(horas.begin(), horas.end(),
          [](auto &a, auto &b){ return a->horasExtras() < b->horasExtras(); });

We copy the Taxista* of your list<Taxista*> in a vector<Taxista*> for two reasons:

  • We do not want to alter the original list.
  • The sorting algorithm does not work in lists but it works in vectors 1 .
  • The example code leaves you in horas taxi drivers ordered according to the extra hours they have made. To make the code even easier, I advise you to overload the injection operator in output data flow for Taxista :

    std::ostream <<(std::ostream &o, const Taxista &t)
    {
        o << "Nombre: " << t. getNombre() << ' ' << t.getApellido() << ' ';
        o << "Sexo: " << (t.getSexo() ? "Femenino" : "Masculino") << ' ';
        o << "Cantidad de horas extra: " << t.horasExtras() << '\n';
    
        return o;
    }
    

    With these changes your reporting function of overtime could look like:

    void ReporteHorasExtra() {
    
        std::vector<Taxista*> horas(taxistas.size());
        std::copy(taxistas.begin(), taxistas.end(), horas.begin());
    
        std::sort(horas.begin(), horas.end(),
                  [](auto &a, auto &b){ return a->horasExtras() < b->horasExtras(); });
    
        for (const auto &t : horas)
            std::cout << *t << '\n';
    }
    

    Much easier to understand, right?

  • If you want to know why, I encourage you to ask another question.
  • answered by 29.11.2018 в 08:19