return x.sort() is wrong. Sort first, then return sorted array.
In new_value = value, new_value is reference. Use deepcopy from copy to copy value.
It has to be trapz(y, x) and not trapz(x, y)
y_new = y is reference. Instead y_new = np.zeros(len(x)) will create an array with all entries are zero.
Corrected figure.