1
$\begingroup$

So I have two lists, A and B. I put them into lists = {A,B}I want to sort A such that Im[#1]<Im[#2]& which I can do fine through Sort[] .The problem is that I want to keep the order the same so that A[[1]] stays with B[[1]] and A[[n]] stays with B[[n]]. When I try SortBy[lists\[Transpose],Im] it does not sort in the same order as the sort function does, but it keeps the A[[n]] with the B[[n]]

EDIT::Example (I still can't figure out how to nicely format)

A={0.651301, 0.671298, -0.000107956 + 0.735512 I,  9.93631*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}



B={{-2.44558*10^-6 - 1.01111*10^-6 I, -5.65309*10^-6 + 0.000018407 I, 
  0.000141659 + 0.0000359384 I, 
  0.000116428 - 0.000796561 I, -0.00506101 - 0.000580627 I, 
  0.000125772 + 0.0163992 I, 0.0817805 + 9.02056*10^-16 I, 
  0.000126129 + 0.0177878 I, 0.00153561 - 0.000581627 I, 
  0.000126597 + 0.000582107 I, -9.00118*10^-7 - 
   0.0000107003 I, -0.0000285126 - 0.0000117884 I, -0.0000659084 + 
   0.000214604 I, 0.00165158 + 0.000419 I, 
  0.00135741 - 0.00928697 I, -0.0590055 - 0.00676943 I, 
  0.00146635 + 0.191195 I, 0.953465 + 0. I, 0.00147052 + 0.207385 I, 
  0.0179034 - 0.00678109 I, 
  0.00147597 + 0.00678669 I, -0.0000104943 - 
   0.000124753 I}, {0.000116916 - 0.000811922 I, -0.00509776 - 
   0.000578293 I, 0.000129979 + 0.0163556 I, 
  0.079143 - 1.63203*10^-14 I, 0.000130143 + 0.0177879 I, 
  0.00147459 - 0.000580172 I, 
  0.00013056 + 0.00059606 I, -2.43588*10^-6 - 0.0000107979 I, 
  4.37224*10^-6 + 9.43651*10^-6 I, -3.88572*10^-7 + 7.61008*10^-9 I, 
  7.14058*10^-8 + 9.11373*10^-8 I, 
  0.00140496 - 0.00975669 I, -0.0612586 - 0.00694921 I, 
  0.00156193 + 0.196542 I, 0.951044 + 0. I, 0.0015639 + 0.213753 I, 
  0.0177198 - 0.0069718 I, 
  0.00156891 + 0.00716271 I, -0.0000292714 - 0.000129756 I, 
  0.0000525402 + 0.000113396 I, -4.66939*10^-6 + 9.14486*10^-8 I, 
  8.58068*10^-7 + 1.09518*10^-6 I}, {-0.00335176 - 0.00246906 I, 
  0.000695327 + 0.0163725 I, -0.00001055 - 
   0.0718776 I, -0.000733202 + 0.0162968 I, 
  0.00326678 - 0.00242532 I, -0.000740926 + 0.000255124 I, 
  0.00011033 + 0.0000490118 I, -0.0000121133 - 0.0000148216 I, 
  2.39696*10^-8 + 2.36464*10^-6 I, 
  1.63148*10^-7 - 2.69095*10^-7 I, -3.15697*10^-8 + 1.27853*10^-8 I, 
  0.0325148 - 0.0441254 I, -0.215566 + 0.00912321 I, 
  0.94636 + 0. I, -0.214567 - 0.00968502 I, 
  0.0319261 + 0.0430159 I, -0.0033576 - 0.00975571 I, -0.000645516 + 
   0.00145253 I, 
  0.000195169 - 0.000159459 I, -0.0000311336 + 3.1102*10^-7 I, 
  3.54266*10^-6 + 2.14857*10^-6 I, -1.68274*10^-7 - 
   4.1568*10^-7 I}, {-1.79728*10^-7 - 
   3.0708*10^-7 I, -8.50791*10^-8 + 2.61365*10^-6 I, 
  0.0000132867 - 0.0000158544 I, -0.000117531 + 0.0000495126 I, 
  0.000766382 + 0.000269089 I, -0.00325494 - 0.0024853 I, 
  0.000752542 + 0.0162187 I, 
  8.95415*10^-7 - 0.0689027 I, -0.000751796 + 0.0162166 I, 
  0.0032569 - 0.00249056 I, -0.000776443 + 0.000265907 I, 
  4.20299*10^-6 - 2.46001*10^-6 I, -0.0000357732 - 1.16402*10^-6 I, 
  0.000217003 + 0.000181853 I, -0.000677703 - 
   0.00160864 I, -0.00368289 + 0.0104896 I, 
  0.0340159 - 0.0445509 I, -0.221987 + 0.010303 I, 
  0.943075 + 0. I, -0.221957 - 0.010287 I, 
  0.0340891 + 0.0445769 I, -0.00363963 - 
   0.0106272 I}, {-0.0000127475 + 0.0000447811 I, 
  0.000264418 + 0.0000598865 I, 
  0.000164152 - 0.0012308 I, -0.00570662 - 0.000548632 I, 
  0.000189568 + 0.0156506 I, 0.0523487 - 2.28983*10^-16 I, 
  0.000191336 + 0.0176777 I, 0.000560154 - 0.000551883 I, 
  0.000187274 + 0.000764703 I, -0.0000430526 - 5.84824*10^-6 I, 
  8.05585*10^-6 + 0.0000148798 I, -0.000220415 + 0.000774304 I, 
  0.00457202 + 0.00103549 I, 
  0.00283833 - 0.0212815 I, -0.0986724 - 0.00948632 I, 
  0.0032778 + 0.270612 I, 0.905154 + 0. I, 0.00330836 + 0.305662 I, 
  0.00968555 - 0.00954253 I, 
  0.00323813 + 0.0132224 I, -0.000744416 - 0.000101121 I, 
  0.000139292 + 0.000257285 I}, {0.000262801 + 0.0000592442 I, 
  0.000164475 - 0.00123486 I, -0.00571449 - 0.00054799 I, 
  0.000190521 + 0.015638 I, 0.0520548 - 1.61329*10^-16 I, 
  0.000192306 + 0.0176744 I, 0.000545908 - 0.000551356 I, 
  0.000188135 + 0.000766815 I, -0.0000438809 - 5.72083*10^-6 I, 
  8.14335*10^-6 + 0.0000150479 I, -1.6632*10^-6 + 4.54164*10^-7 I, 
  0.00456548 + 0.00102921 I, 
  0.00285732 - 0.0214524 I, -0.0992743 - 0.00951989 I, 
  0.0033098 + 0.27167 I, 0.904315 + 0. I, 0.00334081 + 0.307047 I, 
  0.00948372 - 0.00957837 I, 
  0.00326835 + 0.0133214 I, -0.000762316 - 0.0000993844 I, 
  0.000141469 + 0.000261419 I, -0.0000288938 + 
   7.88991*10^-6 I}, {0.00324399 + 0.0151276 I, 
  0.000797234 - 0.0328468 I, -0.00205405 + 0.0139652 I, 
  0.00309583 - 0.0034084 I, -0.00125203 + 0.000542748 I, 
  0.000308022 + 0.0000414382 I, -0.0000545185 - 0.0000426274 I, 
  4.65124*10^-6 + 0.0000121837 I, 
  4.78853*10^-7 - 2.32735*10^-6 I, -2.68691*10^-7 + 2.83621*10^-7 I, 
  6.02922*10^-8 - 1.38336*10^-8 I, -0.382907 + 0.0918831 I, 
  0.836255 + 0. I, -0.356603 - 0.0436393 I, 
  0.088636 + 0.076666 I, -0.014583 - 0.0315217 I, -0.000864141 + 
   0.00786298 I, 
  0.00105095 - 0.00141351 I, -0.000307133 + 0.000125871 I, 
  0.0000595133 + 0.0000107468 I, -7.38245*10^-6 - 6.66148*10^-6 I, 
  3.8922*10^-7 + 1.52555*10^-6 I}, {2.82086*10^-7 + 
   2.97789*10^-7 I, -4.59823*10^-7 - 
   2.41021*10^-6 I, -5.01508*10^-6 + 0.0000122796 I, 
  0.0000564073 - 0.0000412174 I, -0.000307185 + 0.000029994 I, 
  0.00120889 + 0.000596331 I, -0.00288513 - 0.00343464 I, 
  0.00114029 + 0.0135455 I, 
  0.0000642005 - 0.0325252 I, -0.0009149 + 0.0138614 I, 
  0.00312207 - 0.00369177 I, -7.71767*10^-6 + 7.33966*10^-6 I, 
  0.0000625579 - 0.0000120629 I, -0.000319098 - 0.000129588 I, 
  0.00107311 + 0.00146251 I, -0.000794541 - 
   0.00797456 I, -0.0154218 + 0.0314195 I, 
  0.089033 - 0.0750887 I, -0.351653 + 0.030302 I, 
  0.844524 + 0. I, -0.359961 - 0.0230451 I, 
  0.0960172 + 0.0808758 I}, {-0.000892536 - 0.0253483 I, 
  0.00398093 + 0.0167517 I, 
  0.00137243 - 0.00591337 I, -0.0015116 + 0.00179952 I, 
  0.000585214 - 0.000258793 I, -0.000165802 - 0.0000248498 I, 
  0.0000301279 + 0.0000234092 I, -2.58394*10^-6 - 
   7.57878*10^-6 I, -3.51243*10^-7 + 1.52852*10^-6 I, 
  2.06661*10^-7 - 1.94772*10^-7 I, -4.87722*10^-8 + 8.31709*10^-9 I, 
  0.808747 + 0. I, -0.538275 + 0.10806 I, 
  0.186895 + 0.0503687 I, -0.0556474 - 0.0501875 I, 
  0.00759005 + 0.0189387 I, 
  0.000977896 - 0.00525555 I, -0.000779761 + 0.000933787 I, 
  0.000244404 - 0.000073836 I, -0.0000483136 - 0.0000129077 I, 
  5.9747*10^-6 + 6.80398*10^-6 I, -2.10308*10^-7 - 
   1.5635*10^-6 I}, {-2.51454*10^-7 - 1.09646*10^-7 I, 
  8.24658*10^-7 + 1.17552*10^-6 I, -5.98381*10^-7 - 
   7.07371*10^-6 I, -0.0000140755 + 0.0000294825 I, 
  0.000112869 - 0.0000806044 I, -0.000523179 + 0.0000579476 I, 
  0.00169884 + 0.000648417 I, -0.00347909 - 0.00374662 I, 
  0.00369489 + 0.0120913 I, -0.00129613 - 0.020651 I, -0.00517517 + 
   0.0120722 I, 
  4.45129*10^-6 - 8.67954*10^-6 I, -0.0000435543 + 0.0000266477 I, 
  0.000252369 - 5.47974*10^-6 I, -0.00101494 - 0.00056519 I, 
  0.00260914 + 0.00418509 I, -0.000891151 - 0.018696 I, -0.0267954 + 
   0.0588452 I, 0.140712 - 0.115123 I, -0.437333 + 0.104194 I, 
  0.735762 + 0. I, -0.4169 - 0.210549 I}, {1.30427*10^-7 + 
   1.21266*10^-7 I, -2.01494*10^-7 - 
   8.56205*10^-7 I, -1.39899*10^-6 + 3.95881*10^-6 I, 
  0.0000142534 - 0.0000114838 I, -0.0000753911 + 9.27582*10^-6 I, 
  0.000259802 + 0.000101574 I, -0.000571574 - 0.000745225 I, 
  0.000756398 + 0.00272191 I, 
  0.00135245 - 0.0061366 I, -0.00737014 + 0.0139213 I, 
  0.00132698 - 0.0177768 I, -4.48592*10^-6 + 5.61005*10^-6 I, 
  0.0000338329 - 0.0000106751 I, -0.00016343 - 0.0000443834 I, 
  0.000504691 + 0.000538816 I, -0.000599442 - 
   0.0030045 I, -0.00330544 + 0.0107546 I, 
  0.028258 - 0.025227 I, -0.107208 + 0.0385957 I, 
  0.250884 + 0.035973 I, -0.582065 - 0.254641 I, 0.718994 + 0. I}}
$\endgroup$
6
  • $\begingroup$ An example, if you don't mind? $\endgroup$ Commented Jun 3, 2013 at 17:31
  • $\begingroup$ I just threw it in there. Like I said, Sort[] gives me teh order I want in A, but the B doesnt sort with it. SortBy[] does not sort A the way I want it to $\endgroup$ Commented Jun 3, 2013 at 17:37
  • $\begingroup$ Also, sorry for the mess of code, still trying to figure out how to do that. $\endgroup$ Commented Jun 3, 2013 at 17:37
  • $\begingroup$ You can hilight inline code like this by enclosing the text with ` or multiple lines by preceeding each line with 4 spaces (hilight the code and click the "{}" tool in the editor does this for you) $\endgroup$
    – ssch
    Commented Jun 3, 2013 at 17:41
  • $\begingroup$ Try Ordering in order to get the order of the Sort and then apply it to B. $\endgroup$
    – bill s
    Commented Jun 3, 2013 at 18:01

1 Answer 1

6
$\begingroup$

You can use Ordering to get the new ordering of a and then rearrange both a and b accordingly:

a = RandomComplex[1 + 1 I, {100, 1}];
b = RandomComplex[1 + 1 I, {100, 1}];

o = Ordering[a, All, Im[#1] < Im[#2] &];
sa = a[[o]];
sb = b[[o]];
$\endgroup$
1
  • $\begingroup$ Thank you. thank you. thank you $\endgroup$ Commented Jun 3, 2013 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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