Re: sorting complex number?
- To: mathgroup at smc.vnet.net
- Subject: [mg57492] Re: sorting complex number?
- From: "Carl K. Woll" <carlw at u.washington.edu>
- Date: Sun, 29 May 2005 21:00:09 -0400 (EDT)
- Organization: University of Washington
- References: <d79f0f$ldr$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
"Sean2" <sshiau2 at wisc.edu> wrote in message news:d79f0f$ldr$1 at smc.vnet.net... > 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 following: 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