Hint 1: After calculating all the distances in D (see the code below) you can use np.argsort to sort the points in increasing distance.
Hint 2: You can assume that there are only two possible class labels, 0 and 1. As this is the case, you can get the majority where class by np.round(np.mean(y)) where y is the list containing the class labels of the nearest neighbors. This works by calculating the mean of the class labels and rounding it to zero in case it is less than 0.5 and one otherwise, which is the same as the majority rule.