Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #6683

Re: Sorting strings

From Bill Rowe <readnews@sbcglobal.net>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Sorting strings
Date 2011-11-24 12:01 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jalblv$sgt$1@smc.vnet.net> (permalink)

Show all headers | View raw


On 11/23/11 at 7:07 AM, tmatsoukas@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}

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Sorting strings Bill Rowe <readnews@sbcglobal.net> - 2011-11-24 12:01 +0000

csiph-web