The Way to a Friends House Is Never Too Long

Can someone post the algorithm to solve this problem ?

Modify merge sort to get the required order of points and then calculate distance.
In merge sort when the value of x coordinates become equal, check for y coordinates to arrange in required order.

struct Points{
    int x;
    int y;
};
void merge(int b,int m,int e){
    Points temp[n];
    int i=b,j=m+1,k=b;
    while(i<=m && j<=e){
        if(p[i].x<p[j].x)
            temp[k++] = p[i++];
        else if(p[j].x<p[i].x)
            temp[k++]=p[j++];
        else if(p[i].x==p[j].x){
            if(p[i].y>=p[j].y)
                temp[k++]=p[i++];
            else if(p[j].y>p[i].y)
                temp[k++]=p[j++];
        }
    }
    while(i<=m)
        temp[k++]=p[i++];
    while(j<=e)
        temp[k++]=p[j++];
    for(int i=b;i<=e;i++)
        p[i]=temp[i];
}
void merge_sort(int b,int e){
    if(b<e){
        int m=(b+e)/2;
        merge_sort(b,m);
        merge_sort(m+1,e);
        merge(b,m,e);
    }
}