2
$\begingroup$

Okay, so I posted something about sorting, and it was linked as a duplicate to This post However, I am having problems trying to sort a list that contains both real and complex numbers. I put them into one big list and used SortBy[] but that does not sort the complex numbers as I want them to. Any ideas? I can post what code I am using if that helps.

EDIT:: Data

A=
 {{0.651301, 0.671298, -0.000107956 + 0.735512 I, 
  9.93642*10^-6 + 0.764605 I, 0.965926, 0.97048, 0.0344992 + 1.4214 I,
   0.0028631 + 1.4505 I, -0.0626803 + 1.78014 I, -0.12443 + 1.98252 I,
   0.167724 + 
   2.24691 I}, {{-6.79317*10^-6 - 2.8086*10^-6 I, -0.0000157028 + 
    0.0000511297 I, 0.000393492 + 0.0000998273 I, 
   0.000323404 - 0.00221263 I, -0.0140581 - 0.00161283 I, 
   0.00034936 + 0.0455525 I, 0.227164 - 6.59195*10^-17 I, 
   0.000350353 + 0.0494098 I, 0.0042655 - 0.0016156 I, 
   0.000351651 + 0.00161694 I, -2.50028*10^-6 - 
    0.0000297226 I, -0.0000277994 - 0.0000114935 I, -0.0000642597 + 
    0.000209235 I, 0.00161027 + 0.000408518 I, 
   0.00132345 - 0.00905464 I, -0.0575294 - 0.00660009 I, 
   0.00142967 + 0.186412 I, 0.929613 + 0. I, 0.00143373 + 0.202197 I, 
   0.0174555 - 0.00661145 I, 
   0.00143904 + 0.00661691 I, -0.0000102317 - 
    0.000121632 I}, {0.000325231 - 0.00225856 I, -0.0141806 - 
    0.00160866 I, 0.000361568 + 0.0454971 I, 
   0.220155 - 6.245*10^-17 I, 0.000362024 + 0.0494813 I, 
   0.00410192 - 0.00161389 I, 
   0.000363184 + 0.00165808 I, -6.77596*10^-6 - 0.000030037 I, 
   0.0000121624 + 0.0000262499 I, -1.0809*10^-6 + 2.11658*10^-8 I, 
   1.98638*10^-7 + 2.53517*10^-7 I, 
   0.00137179 - 0.00952634 I, -0.0598123 - 0.00678514 I, 
   0.00152505 + 0.191902 I, 0.92859 + 0. I, 0.00152698 + 0.208706 I, 
   0.0173014 - 0.0068072 I, 
   0.00153187 + 0.00699361 I, -0.0000285803 - 0.000126693 I, 
   0.0000512998 + 0.000110719 I, -4.55912*10^-6 + 8.92749*10^-8 I, 
   8.37833*10^-7 + 1.06931*10^-6 I}, {-0.00936005 - 0.00689504 I, 
   0.00194175 + 0.0457214 I, -0.0000294616 - 
    0.200724 I, -0.00204752 + 0.0455102 I, 
   0.00912273 - 0.00677289 I, -0.00206909 + 0.000712454 I, 
   0.000308104 + 0.000136869 I, -0.0000338275 - 0.0000413905 I, 
   6.69395*10^-8 + 6.60345*10^-6 I, 
   4.55605*10^-7 - 7.51471*10^-7 I, -8.81591*10^-8 + 3.57016*10^-8 I, 
   0.0318708 - 0.0432515 I, -0.211296 + 0.00894252 I, 
   0.927617 + 0. I, -0.210317 - 0.0094932 I, 
   0.0312938 + 0.042164 I, -0.0032911 - 0.0095625 I, -0.000632731 + 
    0.00142376 I, 
   0.000191303 - 0.000156301 I, -0.0000305169 + 3.04872*10^-7 I, 
   3.47251*10^-6 + 2.10602*10^-6 I, -1.6493*10^-7 - 
    4.07439*10^-7 I}, {-5.0264*10^-7 - 
    8.58797*10^-7 I, -2.37939*10^-7 + 7.30947*10^-6 I, 
   0.0000371583 - 0.0000443393 I, -0.000328693 + 0.00013847 I, 
   0.0021433 + 0.000752547 I, -0.00910293 - 0.00695052 I, 
   0.0021046 + 0.0453582 I, 
   2.50419*10^-6 - 0.192697 I, -0.00210251 + 0.0453522 I, 
   0.00910842 - 0.00696524 I, -0.00217144 + 0.000743651 I, 
   4.12576*10^-6 - 2.41481*10^-6 I, -0.0000351158 - 1.14264*10^-6 I, 
   0.000213015 + 0.000178511 I, -0.00066525 - 
    0.00157909 I, -0.00361522 + 0.0102968 I, 
   0.0333908 - 0.0437323 I, -0.217908 + 0.0101137 I, 
   0.925747 + 0. I, -0.217879 - 0.010098 I, 
   0.0334627 + 0.0437578 I, -0.00357275 - 
    0.0104319 I}, {-0.0000358944 + 0.000126095 I, 
   0.000744548 + 0.000168628 I, 
   0.000462218 - 0.00346567 I, -0.0160687 - 0.00154484 I, 
   0.000533786 + 0.0440688 I, 0.147403 + 6.93889*10^-17 I, 
   0.000538762 + 0.0497767 I, 0.00157728 - 0.00155399 I, 
   0.000527326 + 0.00215325 I, -0.000121227 - 0.0000164675 I, 
   0.0000226836 + 0.0000418986 I, -0.000217846 + 0.000765279 I, 
   0.00451873 + 0.00102342 I, 
   0.00280524 - 0.0210335 I, -0.0975223 - 0.00937576 I, 
   0.0032396 + 0.267458 I, 0.894604 + 0. I, 0.0032698 + 0.302099 I, 
   0.00957266 - 0.00943131 I, 
   0.00320039 + 0.0130683 I, -0.000735739 - 0.0000999426 I, 
   0.000137669 + 0.000254286 I}, {0.000740075 + 0.000166838 I, 
   0.000463177 - 0.00347749 I, -0.0160926 - 0.0015432 I, 
   0.000536526 + 0.0440383 I, 0.146592 - 1.38778*10^-17 I, 
   0.000541552 + 0.049773 I, 0.00153733 - 0.00155267 I, 
   0.000529806 + 0.00215943 I, -0.000123573 - 0.0000161104 I, 
   0.0000229325 + 0.0000423765 I, -4.68374*10^-6 + 1.27897*10^-6 I, 
   0.00451276 + 0.00101733 I, 
   0.00282432 - 0.0212047 I, -0.0981278 - 0.00940995 I, 
   0.00327157 + 0.268533 I, 0.893872 + 0. I, 0.00330223 + 0.303501 I, 
   0.0093742 - 0.00946775 I, 
   0.0032306 + 0.0131676 I, -0.000753512 - 0.0000982366 I, 
   0.000139836 + 0.0002584 I, -0.00002856 + 
    7.79878*10^-6 I}, {0.00919186 + 0.0428642 I, 
   0.00225897 - 0.0930717 I, -0.00582016 + 0.0395705 I, 
   0.00877203 - 0.00965772 I, -0.00354763 + 0.00153788 I, 
   0.000872782 + 0.000117415 I, -0.000154478 - 0.000120785 I, 
   0.0000131793 + 0.0000345226 I, 
   1.35683*10^-6 - 6.59455*10^-6 I, -7.61336*10^-7 + 8.03641*10^-7 I, 
   1.70839*10^-7 - 3.9198*10^-8 I, -0.380824 + 0.0913832 I, 
   0.831706 + 0. I, -0.354663 - 0.0434019 I, 
   0.0881538 + 0.0762489 I, -0.0145037 - 0.0313503 I, -0.00085944 + 
    0.0078202 I, 
   0.00104523 - 0.00140582 I, -0.000305462 + 0.000125187 I, 
   0.0000591896 + 0.0000106883 I, -7.34228*10^-6 - 6.62524*10^-6 I, 
   3.87106*10^-7 + 1.51725*10^-6 I}, {7.99461*10^-7 + 
    8.43966*10^-7 I, -1.30319*10^-6 - 6.8308*10^-6 I, -0.0000142133 + 
    0.0000348017 I, 
   0.000159864 - 0.000116815 I, -0.000870595 + 0.0000850061 I, 
   0.00342612 + 0.00169007 I, -0.00817676 - 0.00973414 I, 
   0.0032317 + 0.0383895 I, 
   0.000181951 - 0.0921798 I, -0.00259292 + 0.0392848 I, 
   0.00884829 - 0.0104629 I, -7.67732*10^-6 + 7.30127*10^-6 I, 
   0.0000622308 - 0.0000119998 I, -0.00031743 - 0.00012891 I, 
   0.0010675 + 0.00145486 I, -0.000790386 - 
    0.00793286 I, -0.0153412 + 0.0312552 I, 
   0.0885675 - 0.0746961 I, -0.349814 + 0.0301436 I, 
   0.840108 + 0. I, -0.358079 - 0.0229246 I, 
   0.0955152 + 0.0804529 I}, {-0.00253399 - 0.0719661 I, 
   0.0113022 + 0.0475596 I, 
   0.00389645 - 0.0167886 I, -0.00429156 + 0.00510902 I, 
   0.00166148 - 0.000734737 I, -0.000470727 - 0.0000705509 I, 
   0.0000855359 + 0.000066461 I, -7.33604*10^-6 - 
    0.0000215169 I, -9.97209*10^-7 + 4.33961*10^-6 I, 
   5.86732*10^-7 - 5.52976*10^-7 I, -1.38467*10^-7 + 2.36119*10^-8 I, 
   0.805934 + 0. I, -0.536402 + 0.107684 I, 
   0.186245 + 0.0501935 I, -0.0554538 - 0.0500129 I, 
   0.00756365 + 0.0188729 I, 
   0.000974495 - 0.00523727 I, -0.000777049 + 0.000930539 I, 
   0.000243554 - 0.0000735791 I, -0.0000481455 - 0.0000128628 I, 
   5.95393*10^-6 + 6.78033*10^-6 I, -2.09565*10^-7 - 
    1.55805*10^-6 I}, {-7.14387*10^-7 - 3.11508*10^-7 I, 
   2.34287*10^-6 + 3.33967*10^-6 I, -1.70002*10^-6 - 
    0.0000200966 I, -0.0000399888 + 0.0000837606 I, 
   0.000320663 - 0.000228999 I, -0.00148637 + 0.000164631 I, 
   0.00482644 + 0.00184217 I, -0.00988419 - 0.0106442 I, 
   0.0104973 + 0.0343516 I, -0.00368235 - 0.0586699 I, -0.0147028 + 
    0.0342975 I, 
   4.43882*10^-6 - 8.65524*10^-6 I, -0.0000434324 + 0.000026573 I, 
   0.000251662 - 5.46439*10^-6 I, -0.0010121 - 0.000563607 I, 
   0.00260183 + 0.00417338 I, -0.000888655 - 
    0.0186437 I, -0.0267204 + 0.0586804 I, 
   0.140318 - 0.114801 I, -0.436109 + 0.103903 I, 
   0.733702 + 0. I, -0.415733 - 0.20996 I}, {3.70775*10^-7 + 
    3.44736*10^-7 I, -5.72806*10^-7 - 
    2.43401*10^-6 I, -3.97703*10^-6 + 0.0000112541 I, 
   0.0000405196 - 0.0000326462 I, -0.000214321 + 0.0000263692 I, 
   0.000738563 + 0.000288755 I, -0.00162487 - 0.00211852 I, 
   0.00215028 + 0.00773783 I, 
   0.00384473 - 0.0174451 I, -0.0209518 + 0.0395755 I, 
   0.00377232 - 0.0505358 I, -4.47615*10^-6 + 5.59781*10^-6 I, 
   0.0000337592 - 0.0000106518 I, -0.000163074 - 0.0000442868 I, 
   0.000503592 + 0.000537642 I, -0.000598135 - 
    0.00299795 I, -0.00329824 + 0.0107312 I, 
   0.0281964 - 0.0251721 I, -0.106975 + 0.0385116 I, 
   0.250337 + 0.0358946 I, -0.580797 - 0.254086 I, 0.717427 + 0. I}}}

I want A[[1]] to be {.9704,.9659,.67129,.6513,-.000107+.73i,.000000994+.76i,.0344+1.42i,....}

Sorry For the mess of data. I am trying to sort eigenvalues and keep the corresponding eigenvector with them

$\endgroup$
3
  • 2
    $\begingroup$ how to sort complex numbers? dictionary order? or by Real part? we don't know, need one more concrete question. $\endgroup$ May 29 '13 at 15:20
  • $\begingroup$ So I want to sort the Real parts in descending order, and then sort so that the imaginary parts are ascending $\endgroup$ May 29 '13 at 15:21
  • 3
    $\begingroup$ Please try to put yourself in an answerer's shoes. Both this one and your latest question simply can't be answered without more information. Think about this before you post and make sure your questions are clear. "but that does not sort the complex numbers as I want them to" <-- how should we know how you want them to be sorted? There's no standard way to sort complex numbers. If you re-read your question, it should be obvious what's wrong with it. $\endgroup$
    – Szabolcs
    May 30 '13 at 13:56
5
$\begingroup$
SortBy[{1 + I, 5 + I, what + I, 3 + 4 I, 5}, Re] 
{1+I,3+4 I,5,5+I,I+what}
SortBy[{1 + I, 5 + I, what + I, 3 + 4 I, 5}, Re] // Reverse
{I+what,5+I,5,3+4 I,1+I}
SortBy[{1+I,5+I,what+I,3+4I,5}, Im]
{5,1+I,5+I,3+4 I,I+what}

hehe! A=Table[Reverse[Select[A[[i]],Element[#,Reals]&]] ~Join~ SortBy[Select[A[[i]],!Element[#,Reals]&],Im],{i,Length@A}];

In[47]:= A[[1]]

Out[47]= {0.651301,0.671298,0.965926,0.97048,-0.000107956+0.735512 I,9.93642*10^-6+0.764605 I,0.0344992 +1.4214 I,0.0028631 +1.4505 I,-0.0626803+1.78014 I,-0.12443+1.98252 I,0.167724 +2.24691 I}
$\endgroup$
5
  • $\begingroup$ I am trying to do it for an arbitraryset of values. If I have lets say a list named list, that contains: 5, 1+2i, 4, -2 +6i, will that get them in the order 5,4,1+2i,-2+6i ? $\endgroup$ May 29 '13 at 15:42
  • $\begingroup$ I don't know, you can post some sample data. $\endgroup$ May 29 '13 at 15:44
  • $\begingroup$ Sorry for the mess of data I edited in $\endgroup$ May 29 '13 at 15:51
  • $\begingroup$ one way is sort reals and complex seperately and Join them $\endgroup$ May 29 '13 at 16:04
  • $\begingroup$ Thank you! This helped a bunch! $\endgroup$ May 29 '13 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.