[Numpy-discussion] Sorting objects with ndarrays

Pauli Virtanen pav@iki...
Sun Feb 28 05:05:15 CST 2010

su, 2010-02-28 kello 10:25 +0100, Gael Varoquaux kirjoitti:
> The problem is that ndarrays cannot be compared. So I have tried to
> override the 'cmp' in the 'sorted' function, however I am comparing
> fairly complex objects, and I am having a hard time predicting wich
> member of the object will contain the array. 

I don't understand what "predicting which member of the object" means?
Do you mean that in the array, you have classes that contain ndarrays as
their attributes, and the classes have __cmp__ implemented?

If not, can you tell why

        def xcmp(a, b):
            a_nd = isinstance(a, ndarray)
            b_nd = isinstance(b, ndarray)
            if a_nd and b_nd:
                pass # compare ndarrays in some way
            elif a_nd:
                return 1  # sort ndarrays first
            elif b_nd:
                return -1 # sort ndarrays first
                return cmp(a, b) # ordinary compare

does not work?


More information about the NumPy-Discussion mailing list