#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <iomanip>


struct point
{
    int x, y, z;
};

double distance(point l, point r)
{
    int x = l.x - r.x;
    int y = l.y - r.y;
    int z = l.z - r.z;
    return std::sqrt(x*x + y*y + z*z);
}

bool orderByDist(point l, point r, point R)
{
    return distance(l, R) < distance(r, R);
}

int main()
{
    //Initial array
    point arr[5] = {{1,1,1}, {1,2,3}, {-2, 5, 7}, {0, 5, -3}, {-1, 4, -2}};
    //Reference point
    point Ref = {1,1,1};

    using namespace std::placeholders;
    auto pred = std::bind(orderByDist, _1, _2, Ref);

    std::sort(arr, arr + 5, pred);

    //Output
    for(point s: arr)
        std::cout << std::setw(3) << s.x <<
                     std::setw(3) << s.y <<
                     std::setw(3) << s.z <<
                     std::setw(6) << std::setprecision(3) << distance(s, Ref) << std::endl;
}
