Re: Sorting

*To*: mathgroup at smc.vnet.net*Subject*: [mg22084] Re: [mg22067] Sorting*From*: "David Park" <djmp at earthlink.net>*Date*: Sun, 13 Feb 2000 01:14:02 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

>Hi folks, > >can anyone tell me how I can make Mathematica sort variable names >without sorting any numbers within those variables as strings as >well? If you try Sort[{z2,a5,z10}] you get back {a5,z10,z2}, while I >want to get back {a5,z2,z10}. > >Thanks. >Wagner Truppel >wtruppel at uci.edu > Hi Wagner, If I was going to do a lot of this, my first choice would be to use variable names like z[2], a[5] and z[10]. Then Sort[{z[2], a[5], z[10]}] {a[5], z[2], z[10]} You could also use subscripts. However, if you absolutely must have the numbers mixed into the symbol, and if you always have only one leading character, then the following routine will work: symsort[syms_List] := Sort[syms /. s_Symbol /; s =!= List :> Module[{str}, str = ToString[s]; ToExpression[StringTake[str, 1]][ ToExpression[StringDrop[str, 1]]]]] /. (s_)[n_] :> StringJoin[ToString[s], ToString[n]] symsort[{z2, a5, z10}] {"a5", "z2", "z10"} If you have the possibility of more than one leading character, then you must test to find where to break the string. David Park djmp at earthlink.net http://home.earthlink.net/~djmp/