Sorting arrays

Hey there, I've just started learning about arrays and I by the help of internet created a code that sorts everything from the lowest to the highest. My question is how the sort function works and the for loop? Does the sort function always sorts everything from the lowest to the highest ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <algorithm>
using namespace std;
const int SIZE = 6;
int main()
{
    int array[SIZE] = {-1, -5, 6, 4, 2, 6};
    sort(array, array + SIZE);
    for(size_t i = 0; i != SIZE; ++i)
    {
        cout << array[i] << endl;
    }
    return 0;
}
Does the sort function always sorts everything from the lowest to the highest ?


std::sort sorts according to a comparison function. If you don't provide a comparison function, it uses a default one. The default is operator<
It sorts the elements from lowest to highest by default but you can change the order by providing a third argument.

 
std::sort(array, array + SIZE, std::greater<>()); // sort from greatest to lowest 
Topic archived. No new replies allowed.