MathGroup Archive 2011

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

Search the Archive

Re: Sorting strings


On Nov 23, 4:12 am, Themis Matsoukas <tmatsou... 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

Drop the "a", convert to numeric, sort, convert to ascii,
and re-attach the "a".

In[1]:=
"a"<>ToString@#&/@Sort[ToExpression@StringDrop[#,1]&/@
{"a1", "a2", "a20", "a12"}] //InputForm

Out[1]//InputForm=
{"a1", "a2", "a12", "a20"}



  • Prev by Date: Re: Sorting strings
  • Next by Date: Re: Sorting strings
  • Previous by thread: Re: Sorting strings
  • Next by thread: Re: Sorting strings