MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: sorting complex number?

  • To: mathgroup at
  • Subject: [mg57492] Re: sorting complex number?
  • From: "Carl K. Woll" <carlw at>
  • Date: Sun, 29 May 2005 21:00:09 -0400 (EDT)
  • Organization: University of Washington
  • References: <d79f0f$ldr$>
  • Sender: owner-wri-mathgroup at

"Sean2" <sshiau2 at> wrote in message 
news:d79f0f$ldr$1 at
> Hi, I need some help
>  I am trying to sort a list of complext number, say, List={a+b*I, c+d*I};
> and I want a>c and b>d. I know that Sort can do this with a small list of 
> complex numbers, but what I deal with is a list of about 100000 and maybe 
> more, and it doesn't seem to work with the imaginary part when you just 
> Sort[List],
> Does anyone have a small program to do this?
> thank you

It seems that Mathematica sorts complex numbers first by the real part, and 
then by the magnitude of the complex number, and then by the sign, e.g.:

Sort[{5 + 5 I, 5 - 10 I}]
{5 + 5 I, 5 - 10 I}

One possibility is to do as David Park suggests, and use Sort with an 
ordering function p. Unfortunately, in your case you are interested in lists 
of about 100000 elements, and sorting with an ordering function is much 
slower than sorting without an ordering function. On my machine, sorting a 
100000 element list with David's ordering function took several minutes.

As I wrote in an earlier post today, a much quicker alternative to sorting 
with an ordering function is to use Ordering. For your case, we would do the 

data[[ Ordering[ Transpose[{Re[#],Im[#]}] ] ]] &[N@data]

On my machine, when data was a 100000 element list, the above computation 
took less than 2 seconds.

Carl Woll 

  • Prev by Date: Re: sorting complex number?
  • Next by Date: Re: Re: FindRoot: Failed to converge to the
  • Previous by thread: Re: sorting complex number?
  • Next by thread: FindRoot:: Failed to converge to the requested accuracy