MathGroup Archive 2011

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

Search the Archive

Re: Sorting strings

On 11/23/11 at 7:07 AM, tmatsoukas at (Themis Matsoukas) 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?

Here is one way

In[1]:= list = {"a1", "a2", "a20", "a12"};

In[2]:= list[[Ordering[ToExpression[StringReplace[list, "a" -> ""]]]]]

Out[2]= {a1,a2,a12,a20}

  • Prev by Date: how to project vertex along a tangent
  • Next by Date: Possible Radon[] bug
  • Previous by thread: Re: Sorting strings
  • Next by thread: Re: Sorting strings