Ideally sort() function is superb example of Polymorphism. In case of sort() function, you can sort almost anything with it.
In [27]: b
Out[27]: [3, 4, 5, 6]
In [28]: b = ['a','b',5,6,None]
In [29]: b.sort()
In [30]: b
Out[30]: [None, 5, 6, 'a', 'b']
In [31]: b = ['a','b',23,'c',None,5j]
In [32]: b.sort()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/dubizzle/webapps/django/dubizzle/<ipython-input-32-fc40da74ac51> in <module>()
----> 1 b.sort()
TypeError: no ordering relation is defined for complex numbers
but it seems that in case of imaginary numbers sort() function fails. Please note that I am getting this error TypeError: no ordering relation is defined for complex numbers.
So My questions are
- Where exactly this ordering is defined ? How sort() function works internally ?
- Is there any purpose for leaving this ordering relation for complex numbers or it just left out similarly as we have no power operator in c language ( A mistake).
- How do we sort the imaginary (complex) numbers in python basically ? Do we have a pythonic way to do this ?
In mathematical terms, the set of complex numbers cannot be a totally ordered set; defining an order for them requires you to provide a key by which they can be ordered. This depends on what you want: do you want to order them by their real values?
By their imaginary values?
By their magnitude?
However you want to order them, you specify a function to compare elements using the key keyword.