Re: Sorting a list of symbols
- To: mathgroup at smc.vnet.net
- Subject: [mg116863] Re: Sorting a list of symbols
- From: Albert Retey <awnl at gmx-topmail.de>
- Date: Wed, 2 Mar 2011 04:37:48 -0500 (EST)
- References: <ikihp7$7tc$1@smc.vnet.net>
Hi,
> Is there a way of sorting a list of symbols like {v1, v2, v3, v10, v4} such that
> it gives {v1, v2, v3, v4, v10} and not the Mathematica default of {v1,v10, v2,v3, v4}? I
> am trying to avoid using the obvious solution of {v01, v02, v03, v04, v10}!
well, I'm not sure whether I really would do it, but this should work:
SortBy[
{v1, v2, v3, v10, v4},
ToExpression[
StringReplace[SymbolName[#],
"v" ~~ d : (DigitCharacter ..) ~~ ___ :> d]] &
]
of course depending on the variable names, you might need to adopt the
string pattern...
hth,
albert