ChiaraJ
ChiaraJ

Reputation: 11

How to sort a list by some properties (only one entry of the list) C++

I have a list representing many characteristics of a fish (Trout). One of these characteristics is the length of the organism (L). I coded multiple Trouts, so eventually in my list "Trout" there are many fishes characterized by the same types of property. I would like to sort my list by decreasing order of only one property (length), so that the first object of my list is the longest fish and so on. I could not find an helpful post on C++, I hope somebody can help me to solve faster this problem, thanks!

I have tried to zip together the Trout list with a list of only length, to sub-sequentially sort by Length and then select only the Trout entries, but I did not succeed.

C++

 class Trout {
         public:
              string species;
              double U_E, V, U_H, U_R; 
              double e_scaled, L;
              double SA, SC; 
              int age, mom_birth, mom_metamorph, mom_maturation;
              double scatter; 
              int ag_step;
              double age_acc, h_acc, Hazard; 
              bool spawn;
              int year_reproduction;
              int day_reproduction;
              int day_birth;
              int year_birth;
            double L_j;
            double L_b ;
            double pA_m_surf, Ub_H, Uj_H, Up_H, g, J_X_Am, E_m_vol, pX_m, L_max;

  Trout(string spc, double aa, double bb, double cc, double dd, double ee, double gg, double hh,
                    int oo,int mb, int mom, int mmat, double ind_variability,  double aging_acceleration,
                    double hazard_rate, double Hazard_f, bool spw, int year_r, int day_r, int day_b, int year_b,
                    double MaxassRate, double matB, double matJ, double matP,
                    double EnRatio, double ingRate, double Edensity, double EingRate, double LengthM,
                    double Lb_sc, double Lj_sc);
              virtual ~Trout(){}
              void printFriendInfo();
      };

      Trout::Trout(string spc, double aa, double bb, double cc, double dd, double ee, double gg, double hh,
                   int oo,int mb, int mom, int mmat, double ind_variability ,  double aging_acceleration,
                   double hazard_rate, double Hazard_f, bool spw, int year_r, int day_r, int day_b, int year_b,
                   double MaxassRate, double matB, double matJ, double matP,
                   double EnRatio, double ingRate, double Edensity, double EingRate, double LengthM,
                   double Lb_sc, double Lj_sc)
      {
        species = spc;
        U_E = aa;  U_H = bb;  U_R = cc;
        e_scaled = dd;

        L = ee;

        SA = gg; SC = hh;
        age = oo;
        mom_birth = mb;  mom_metamorph = mom;  mom_maturation = mmat;
        scatter = ind_variability;
        age_acc = aging_acceleration;
        h_acc = hazard_rate;
        Hazard = Hazard_f;
        spawn = spw;
        year_reproduction = year_r;
        day_reproduction = day_r;
        day_birth = day_b;
        year_birth = year_b;
        pA_m_surf = MaxassRate;
        Ub_H = matB;  Uj_H = matJ;   Up_H = matP;
        g = EnRatio;
        J_X_Am = ingRate;
        E_m_vol = Edensity;
        pX_m = EingRate;
        L_max = LengthM;
        L_b = Lb_sc;
        L_j = Lj_sc;
      };

Upvotes: 0

Views: 68

Answers (3)

Armali
Armali

Reputation: 19375

Since you say you have a list, here's another way working with std::list.

Alternatively to defining operator <, one can define a cast operator to a standard type, e. g.:

 class Trout
 {
         public:
            operator double () { return -L; }
            …
 }
 …
     // presumed you have a list <Trout> l, you can now sort it by just doing:
     l.sort();

I negated the return value since you want to sort in decreasing order.

Of cource this is only usable if you only ever want to sort on the one property (length).

Upvotes: 0

Tanveer Badar
Tanveer Badar

Reputation: 5523

It is actually pretty easy to achieve. I am going to assume a few things (and explain them as I go along).

Say you have a vector<Trout> and want to sort it in descending order of each Trout's length. Following code will do the job fine.

vector<Trout> trouts(fill_collection()); // returns a vector of trouts from somewhere.

sort(begin(trouts), end(trouts), [](const auto& a, const auto&b)
{
    return a.length < b.length; // assuming there's a public field length available in trouts.
});

Or you can define a operator<() for your Trout objects which will be automatically picked by sort().

bool operator<(const Trout& a, const Trout& b)
{
    return a.length < b.length;
}

Upvotes: 0

One Man Monkey Squad
One Man Monkey Squad

Reputation: 403

Use std::sort with a custom comparator (in my example with a lambda).

std::vector<Trout> trouts;
auto cmp = [](const Trout& lhs, const Trout& rhs){ return lhs.L < rhs.L; };
std::sort(trouts.begin(), trouts.end(), cmp);

Upvotes: 3

Related Questions