Re: Sorting strings

*To*: mathgroup at smc.vnet.net*Subject*: [mg123144] Re: Sorting strings*From*: Leonid Shifrin <lshifr at gmail.com>*Date*: Thu, 24 Nov 2011 06:58:44 -0500 (EST)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com*References*: <201111231207.HAA14969@smc.vnet.net>

Yes this is possible, although I don't see a very straightforward way of doing that efficiently. This function will be reasonably fast: sortStringVars[vars : {__String}] := With[{split = Flatten[StringCases[vars, ShortestMatch[x__] ~~ y : (DigitCharacter ..) :> {x, ToExpression@y}], 1]}, MapThread[StringJoin, {#[[All, 1]], ToString /@ #[[All, 2]]}] &@ SortBy[split, {First, Last}]] For example, In[104]:= sortStringVars[{"a1", "a2", "a20", "a12"}] Out[104]= {"a1", "a2", "a12", "a20"} Here is a benchmark on larger sample of randomly generated variables: testStrings = Flatten@Outer[StringJoin, CharacterRange["a", "z"], ToString /@ RandomInteger[10000, 5000]]; In[108]:= Length[testStrings] Out[108]= 130000 In[109]:= sortStringVars[testStrings]//Short//Timing Out[109]= {1.17,{a1,a6,a6,a7,a7,a8,<<129989>>,z9997,z9998,z9999,z10000,z10000}} I am not claiming this is the fastest implementation possible in Mathematica, just reasonably fast to not be a major bottleneck. Regards, Leonid On Wed, Nov 23, 2011 at 3:07 PM, Themis Matsoukas <tmatsoukas at me.com> wrote: > Sorting this list of strings > > {"a1", "a2", "a20", "a12"} // Sort > > I get > > {"a1", "a12", "a2", "a20"} > > but I would like the numerals to be sorted as numbers, i.e., as > > {"a1", "a2", "a12", "a20"} > > Is this possible or do I have to rename "a1" into "a01" etc? > > Thanks > > Themis > >

**References**:**Sorting strings***From:*Themis Matsoukas <tmatsoukas@me.com>