sort<>

Algorithm: std::sort

Now for something simple …

Sorting a C array
int int_array[] = { 34, 45, 1, 3, 2, 666 };
std::sort(int_array, int_array + 6);
Sorting a C++ vector
std::vector<int> int_array;
int_array.push_back(42);
int_array.push_back(7);
int_array.push_back(666);

std::sort(int_array.begin(), int_array.end());

Algorithm: std::sort, custom comparison

bool less_reverse(int l, int r)
{
    return l > r;
}

int int_array[] = { 34, 45, 1, 3, 2, 666 };
std::sort(int_array, int_array + 6, less_reverse);

Live Hacking

#include <algorithm>
#include <string>
#include <vector>
#include <cassert>

#include <iostream>
using namespace std;


static bool greater_than(int l, int r)
{
    return l > r;
}

int main()
{
    {
        int array[] = { 34, 45, 1, 3, 2, 666 };
        std::sort(array, array+6);
        
        for (int i=0; i<6; i++)
            cout << array[i] << endl;
    }

    {
        std::vector<std::string> array;
        array.push_back("Joerg");
        array.push_back("Philipp");
        array.push_back("Caro");
        array.push_back("Johanna");

        // '<' is defined on strings (this is why sort() works with
        // strings)
        assert(array[2] < array[0]);
        
        std::sort(array.begin(), array.end());

        for (size_t i=0; i<array.size(); i++)
            cout << array[i] << endl;
    }

    // reverse sort
    {
        int array[] = { 34, 45, 1, 3, 2, 666 };

        std::sort(array, array+6, greater_than);
        
        for (int i=0; i<6; i++)
            cout << array[i] << endl;
    }

    return 0;
}