Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
1  
In which sense " it does not function" ? –  b.gatessucks Oct 14 '13 at 13:50
1  
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). –  ssch Oct 14 '13 at 13:58
    
@ssch, how can I overcome the problem? –  user9994 Oct 14 '13 at 14:13
    
Can you clarify on the ordering? SortBy[list, -Abs[Re[ω /. #]] &] doesn't match your desired output –  ssch Oct 14 '13 at 14:21
    
@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? –  user9994 Oct 14 '13 at 19:22

1 Answer 1

up vote 1 down vote accepted

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}

share|improve this answer
    
thank you very much, it works perfectly. –  user9994 Oct 14 '13 at 19:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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