Re: Sorting strings
- To: mathgroup at smc.vnet.net
- Subject: [mg123129] Re: Sorting strings
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Thu, 24 Nov 2011 06:56:00 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
On 11/23/11 at 7:07 AM, tmatsoukas at me.com (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}