Re: sort list

*To*: mathgroup at smc.vnet.net*Subject*: [mg47289] Re: sort list*From*: "Dana DeLouis" <delouis at bellsouth.net>*Date*: Fri, 2 Apr 2004 03:30:57 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

I like Bill Rowe's solution. Thanks. I changed two custom functions that I was using. 'Some data... v=Table[{ FromCharacterCode[Random[Integer,{97,122}]], Random[Integer,{1,1000}] },{j,10000}]; Timing demo... ans=v[[Ordering[v[[All,2]]]]]//Timing;ans[[1]] 0.11 Second ans=Sort[v, #1[[2]]<#2[[2]]&]//Timing;ans[[1]] 0.852 Second Bill's was faster. Thanks. :>) Taking Bill's idea, "MySort" sorts in increasing order, while "MySortRev" sorts in reverse order. ' = = = = = MySort[v_List,n_Integer]:=v[[Ordering[v[[All,n]]]]] MySortRev[v_List,n_Integer]:=v[[Ordering[v[[All,n]],Length[v],Greater]]] ' = = = = = ' Smaller data set for testing... v=Table[{ FromCharacterCode[Random[Integer,{97,122}]], Random[Integer,{1,1000}] },{j,10}]; MySort[v,2] {{f,16},{s,46},{e,246},{w,364},{n,397},{l,501},{q,568},{e,581},{t,615},{ i,747} } MySortRev[v,2] {{i,747},{t,615},{e,581},{q,568},{l,501},{n,397},{w,364},{e,246},{ s,46},{f,16}} -- -- Dana DeLouis Mathematica 5.0 for Windows delouis at bellsouth.net = = = = = = = = = = = = = = = = = "Bill Rowe" <readnewsciv at earthlink.net> wrote in message news:c4gf8m$2hf$1 at smc.vnet.net... > On 3/30/04 at 4:02 AM, guibout at ifrance.com (Guibout) wrote: > > >Hi, I have a list of the form > >{{something1,x1},{something2,x2},{something3,x3}} where x1,x2, x3 > >are numbers. I want to sort this list with respect to xi. In other > >word if x2<x3<x1 I want Mathematica to produce: > >{{something2,x2},{something3,x3},{something1,x1}} > > There are a number of ways to do this. Either > > Sort[list, OrderedQ[{Last@#1,Last@#2}]&] > > or > > list[[Ordering[list[[All,2]]]]] > > will so what you want. Note, the second of these two methods is much faster for large lists. > -- > To reply via email subtract one hundred and four >