I have a list of integers which I am trying to order in the following way:
- remove all zeros from the list
- sort in increasing order of absolute value
- put positive values before negative values
I.e. the list n={0,1,-3,3,-3,0,12,10,0}
should go to {1,3,-3,-3,10,12}
.
I'm very new to Mathematica and can't figure this out although it's probably simple.
Things I tried:
define a function
simplifyList[n_]:=Sort[n, Less] /. {Longest[0 ...], x___} :> {x}
. This only works when there are no negative values, otherwise zeros remain.Sort[DeleteCases[n, 0], Abs[#1] < Abs[#2] &]
. This returns{1, -3, 3, -3, 10, 12}
with the listn
above, i.e. the negative/positive numbers with same absolute value are not sorted.
Is there a simple way of doing this? Speed is quite important here, as the simplifying-list function will be used 1000s of times, so the more efficient the better.