Re:Sorting and Selecting in Multilevel Lists
- To: mathgroup at smc.vnet.net
- Subject: [mg31179] Re:Sorting and Selecting in Multilevel Lists
- From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
- Date: Tue, 16 Oct 2001 01:18:58 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
aes wanted to sort a list of ordered pairs according to the magnitude of
(x^2+y^2).
The following works well.
In[1]:=
data={{3,1},{1,2},{8,5},{3,2},{1,1}};
Sort[data, First[#1]^2+Last[#1]^2<First[#2]^2+Last[#2]^2& ]
Out[2]=
{{1,1},{1,2},{3,1},{3,2},{8,5}}
----------------------------------------
Here First[] is the x value, and Last[] is the y value.
A general tip; First[expr] is slightly faster than Part[expr,1]. Also
Last[expr] should be faster than anything else that does the same thing.
Regards,
Ted Ersek
Check Mathematica Tips, Tricks at
http://www.verbeia.com/mathematica/tips/Tricks.html