Re: Sort problem
- To: mathgroup at smc.vnet.net
- Subject: [mg56330] Re: Sort problem
- From: Jon Harrop <usenet at jdh30.plus.com>
- Date: Fri, 22 Apr 2005 06:23:06 -0400 (EDT)
- References: <d47suf$54n$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
konstantpi at mail15.com wrote:
> Hi
> How to sort two lists such as:
> age={30,12,60,20};
> pple={"john","kate","peter","alice"};
>
> to give the output:
> age={12,20,30,60}
> pple={"kate","alice","john","peter"}
>
> ie: to keep every person opposite his age number.
> thanks
Combine the two lists into one list of pairs:
In[1]:= data = Transpose[{age, pple}]
Out[1]= {{30, john}, {12, kate}, {60, peter}, {20, alice}}
Then sort by comparing only the first part of each element:
In[1]:= Sort[data, First[#1] < First[#2] &]
Out[1]= {{12, kate}, {20, alice}, {30, john}, {60, peter}}
The extract the ages and people:
In[1]:= {age, pple} = {First /@ data, Last /@ data}
Out[1]= {{30, 12, 60, 20}, {john, kate, peter, alice}}
--
Dr Jon D Harrop, Flying Frog Consultancy
http://www.ffconsultancy.com