MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Sorting strings

  • To: mathgroup at
  • Subject: [mg123144] Re: Sorting strings
  • From: Leonid Shifrin <lshifr at>
  • Date: Thu, 24 Nov 2011 06:58:44 -0500 (EST)
  • Delivered-to:
  • References: <>

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 =
      ShortestMatch[x__] ~~ y : (DigitCharacter ..) :> {x, ToExpression@y}],
  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


I am not claiming this is the fastest implementation possible in
Mathematica, just reasonably fast
to not be a major bottleneck.


On Wed, Nov 23, 2011 at 3:07 PM, Themis Matsoukas <tmatsoukas at> 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

  • Prev by Date: Color discontinuity with ColorFunction in ContourPlot
  • Next by Date: Non trivial substitution in a very long output
  • Previous by thread: Re: Sorting strings
  • Next by thread: Re: Sorting strings