0

I am trying to understand how I could compare imaginary number in a list like so, and 1j is the imaginary number (-1 square root)

   x = [1, 1j]
   x.sort()
    Traceback (most recent call last):
      File "<pyshell#13>", line 1, in ?
        x.sort()
    TypeError: cannot compare complex numbers using <, <=, >, >=
DemarsM
  • 11
  • 2

1 Answers1

2

You can use a bit of mathematics here rather than programming.

Complex numbers cannot be compared normally, but they have to be typically compared by comparing their modulus (sqrt of the sum of the squares of its real and complex parts). You can accordingly define functions for the same.

sgp
  • 1,738
  • 6
  • 17
  • 31
  • I'd like to give you upvote but I need more reputation – DemarsM Jul 22 '14 at 17:55
  • You're welcome, if this answer is correct for you, should close the question; set this answer as your accepted answer @DemarsM – sgp Jul 22 '14 at 20:53