Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In my program it program the array prints from ascending order, but i'm trying to modify it so it prints in descending order [duplicate]

In my program it program the array prints from ascending order, but i'm trying to modify it so it prints in descending order. Could use some help.

#include <iostream>
#include <algorithm>
int main()
{
    const int length = 5;
    int array[length] = {35, 67, 75, 60, 11};

    std::sort(std::begin(array), std::end(array));
    for(int i = 0; i < length; i++)
    {
      std::cout << array[i] << ' ';
    }

    return 0;
}
like image 902
Alex Turner Avatar asked Jan 01 '26 03:01

Alex Turner


1 Answers

std::sort can take 3rd parameter, a comparator, which will say how to order the elements inside the array. You can pass one of standard comparators inside your call to std::sort

std::sort(std::begin(array), std::end(array), std::greater<int>{});
like image 78
Kaldrr Avatar answered Jan 03 '26 16:01

Kaldrr



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!