Re: Sorting and Selecting in MultiLevel Lists?
- To: mathgroup at smc.vnet.net
- Subject: [mg31171] Re: [mg31163] Sorting and Selecting in MultiLevel Lists?
- From: BobHanlon at aol.com
- Date: Tue, 16 Oct 2001 01:18:46 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 2001/10/14 5:29:24 AM, siegman at stanford.edu writes:
>Suppose I want to Sort, or Select from, a multilevel list, e.g.
>
> myList = { {x1,y1}, {x2,y2}, {x3,y3}, . . . }
>
>with a Sort or Select criterion that's some function of the xn and yn values
>
>For example, I can sort the above list on the value of x^2 + y^2 by using
>
> Sort[myList, (Take[#1, 1][[1]]^2 + Take[#1, 2][[1]]^2) <
> (Take[#2, 1][[1]]^2 + Take[#2, 2][[1]]^2) &]
>
>Question: Is there an easier way to get at the "x" and "y" values associated
>
>with the #1 and #2 arguments in Sort, or with the # argument in Select,
>than the
>awkward Take[#,m][[n]] notation used here?
>
data = Table[{Random[], Random[]}, {10}];
You don't need the Take
Sort[data, #1[[1]]^2+#1[[2]]^2 < #2[[1]]^2+#2[[2]]^2&]
However, for the test used, you can write it more compactly as
Sort[data, #1.#1 < #2.#2&]
Bob Hanlon
Chantilly, VA USA