def sort(polylines):
    for n, c in enumerate(polylines):
        l = len(c)
        if l > 2:
            mid = c.mean(axis=0)
            distV = (c - mid)
            dists = norm(distV, axis=-1)
            firstI = np.argmax(dists)
            sign = np.sign(distV[firstI])
            dd = np.logical_or(np.sign(distV[:, 0]) != sign[0],
                               np.sign(distV[:, 1]) != sign[1])
            dists[~dd] = 0
            lastI = np.argmax(dists)
            ind = _sort(c, firstI, lastI)
            c = c[ind]
            polylines[n] = c