How to sort class objects array using the sort() in C++

  c++, class, lambda, object, sorting

I was doing one programming problem where it is required to sort the class objects.

So, i used sort() to sort the array of objects but i couldn’t do it, i read somewhere it requires lambda expression to that, but i don’t know about them yet.

So, if you can help me with this question with some explanation of lambda expression it would be wonderful.

#include<bits/stdc++.h>
using namespace std;

class CallLog{
    public:
    string pno;
    int dur;
    string da;
    void setpno(string p_no) {pno = p_no;}
    void setdur(int d) {dur = d;}
    void setda(string dsa) {da = dsa;}
    string getpno() {return pno;}
    int getdur() {return dur;}
    string getda() {return da;}
};

int main() {
    int n;
    cin >> n;
    CallLog arr[n];
    for(int i = 0; i < n; i++) {
        string pno;
        int dur;
        string da;
        cin >> pno >> dur >> da;
        arr[i].setpno(pno);
        arr[i].setdur(dur);
        arr[i].setda(da);
    }
    sort(arr, arr+n, "what labmda expression to write?" );
    for(int i = 0; i < n; i++) {
        cout << "DialledNumber : " << arr[i].getpno() << ", Duration : " << arr[i].getdur() << ", Date : " << arr[i].getda() << "n";
    }
}

I want to sort these objects with respect to the int dur.

Source: Windows Questions C++

LEAVE A COMMENT