numpy.argsort
returns a sorted list to perform an indirect sorting, but it doesn't seem to accept a user-defined function to compare two elements.
I wonder how one can get the sorted list based on a comparison with a user-defined function.
In my case, I have a table of results:
a = [[1,2,3,5,6,7,8],[4,6,2,5,6,3,4],...]
And my function to compare is:
def argMedian(A):
s = np.array([sum(A[:i+1]) for i in range(Nmentions)])
mid = float(s[Nmentions-1])/2
return np.argwhere(mid < s)[0][0]
def tieBreaking(A, B):
Ac = A
Bc = B
medA = argMedian(Ac)
medB = argMedian(Bc)
while medA == medB:
Ac[medA] -= 1
Bc[medB] -= 1
medA = argMedian(Ac)
medB = argMedian(Bc)
return 1 if medA > medB else -1
This is an implementation of the majority judgment. A list contains the number of votes for each grade. In case of the median between two lists is equal, a median vote is deleted for both lists and the comparison of the median is again tested. Here, I consider index 0 as the best grade and index 7 as the worst.
I need to perform an indirect sorting, because I want to know the ranking.