MathGroup Archive 2011

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

Search the Archive

Re: Sorting strings

  • To: mathgroup at smc.vnet.net
  • Subject: [mg123142] Re: Sorting strings
  • From: "Oleksandr Rasputinov" <oleksandr_rasputinov at hmamail.com>
  • Date: Thu, 24 Nov 2011 06:58:22 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <jainvm$eop$1@smc.vnet.net>

On Wed, 23 Nov 2011 12:12:38 -0000, Themis Matsoukas <tmatsoukas 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
>

You could try:

lst = {"a1", "a12", "a2", "a20"};

nums =
  ToExpression@StringTrim[{"a1", "a12", "a2", "a20"}, LetterCharacter]

{1, 12, 2, 20}

Part[lst, Ordering[nums]]

{"a1", "a2", "a12", "a20"}



  • Prev by Date: Re: Sorting strings
  • Next by Date: ContourPlot ColorFunction workaround
  • Previous by thread: Re: Sorting strings
  • Next by thread: Computational help needed very much!