Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regarding std::list, if I want to sort in descending order can I simply call "mylist.sort(>)"?

Tags:

c++

list

sorting

Assuming that whatever objects the list holds implements a proper > operator.

like image 487
Milan Novaković Avatar asked Oct 27 '25 07:10

Milan Novaković


1 Answers

Use :

mylist.sort(std::greater<T>());

T is type of container elements. eg. int, char, float, etc.

For T as objects you need to overload operator '>' or define your own comparator function

bool cmp(const T &lhs, const T &rhs)
{

  //compare criteria 
}

And then,

mylist.sort(cmp);

like image 174
P0W Avatar answered Oct 29 '25 21:10

P0W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!