0
$\begingroup$

I have a list

list={{ω->0.538826+1.31221 I},{ω->-0.538826+1.31221 I},{ω->-1.2444-0.68023 I},{ω->1.2444-0.68023 I},{ω->0.496864+1.32811 I},{ω->-0.496864+1.32811 I},{ω->1.22187-0.719147 I},{ω->-1.22187-0.719147 I},{ω->0.454442+1.34261 I},{ω->-0.454442+1.34261 I},{ω->1.19811-0.757303 I},{ω->-1.19811-0.757303 I},{ω->1.17316-0.794657 I},{ω->-1.17316-0.794657 I},{ω->0.411617+1.35568 I},{ω->-0.411617+1.35568 I},{ω->-1.14704-0.831174 I},{ω->1.14704-0.831174 I}}

and I would like to sort it by ascending positive. I tried with the Sort command

Sort[list, (Re[#1[[1, 2]]] < Re[#2[[1, 2]]] && Re[#1[[1, 2]]] > 0) &]

but it does not sort them in desired order:

{{ω->1.14704-0.831174 I},{ω->-1.14704-0.831174 I},{ω->-0.411617+1.35568 I},{ω->0.411617+1.35568 I},{ω->-1.17316-0.794657 I},{ω->1.17316-0.794657 I},{ω->-1.19811-0.757303 I},{ω->1.19811-0.757303 I},{ω->-0.454442+1.34261 I},{ω->0.454442+1.34261 I},{ω->-1.22187-0.719147 I},{ω->1.22187-0.719147 I},{ω->-0.496864+1.32811 I},{ω->0.496864+1.32811 I},{ω->1.2444-0.68023 I},{ω->-1.2444-0.68023 I},{ω->-0.538826+1.31221 I},{ω->0.538826+1.31221 I}}

How am I wrong?

Thanks in advance

$\endgroup$
5
  • 1
    $\begingroup$ In which sense " it does not function" ? $\endgroup$ Commented Oct 14, 2013 at 13:50
  • 1
    $\begingroup$ The && Re[#1[[1, 2]]] > 0 part makes this not be a "valid" ordering i.e. the outcome will depend on the initial order of the list (and sorting algorithm used). $\endgroup$
    – ssch
    Commented Oct 14, 2013 at 13:58
  • $\begingroup$ @ssch, how can I overcome the problem? $\endgroup$
    – user9994
    Commented Oct 14, 2013 at 14:13
  • $\begingroup$ Can you clarify on the ordering? SortBy[list, -Abs[Re[ω /. #]] &] doesn't match your desired output $\endgroup$
    – ssch
    Commented Oct 14, 2013 at 14:21
  • $\begingroup$ @ssch, I would like to have the terms with the smallest positive real part first. the answer of Jacob Akkerboom solved my problem. Do you suggest any other solution? $\endgroup$
    – user9994
    Commented Oct 14, 2013 at 19:22

1 Answer 1

1
$\begingroup$

How about first selecting the values that you want to keep and then sorting? For example

limitedList2 = Select[list, Re[#[[1, 2]]] > 0 &];
SortBy[limitedList2, Re[#[[1, 2]]] &]

{{ω->0.411617 +1.35568 I},{ω->0.454442 +1.34261 I},{ω->0.496864 +1.32811 I},{ω->0.538826 +1.31221 I},{ω->1.14704 -0.831174 I},{ω->1.17316 -0.794657 I},{ω->1.19811 -0.757303 I},{ω->1.22187 -0.719147 I},{ω->1.2444 -0.68023 I}}

or if you want a more compact data structure

limitedList = 
 Cases[list, _Complex?(Composition[Positive, Re]), Infinity];
SortBy[limitedList, Re]

{0.411617 + 1.35568 I, 0.454442 + 1.34261 I, 0.496864 + 1.32811 I, 0.538826 + 1.31221 I, 1.14704 - 0.831174 I, 1.17316 - 0.794657 I, 1.19811 - 0.757303 I, 1.22187 - 0.719147 I, 1.2444 - 0.68023 I}

$\endgroup$
1
  • $\begingroup$ thank you very much, it works perfectly. $\endgroup$
    – user9994
    Commented Oct 14, 2013 at 19:21

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.