       Re: Simple String question

• To: mathgroup at smc.vnet.net
• Subject: [mg110723] Re: Simple String question
• From: David Bailey <dave at removedbailey.co.uk>
• Date: Sat, 3 Jul 2010 08:19:01 -0400 (EDT)
• References: <i0k2ft\$jra\$1@smc.vnet.net>

```On 02/07/10 07:54, S. B. Gray wrote:
> I have strings like foo="623 715". foo will always be 3 digits, space, 3
> digits. I need to put each set of digits in numerical order and then put
> the two triples in order. This would give 157 236. There must be a
> simple way to do this, but I see nothing under string sorting.
>

This splits out the two 3-digit codes, splits these into individual
characters using Characters[], and sorts the result before reassembling
each string. Then I compare the numerical value of the two expressions,
and return the appropriate ordering:

In:= foo = "623 715"

Out= "623 715"

In:= str1 = StringTake[foo, 3];

In:= str2 = StringDrop[foo, 4];

In:= str1 = StringJoin @@ Sort@Characters[str1]

Out= "236"

In:= str2 = StringJoin @@ Sort@Characters[str2]

Out= "157"

In:= If[ToExpression[str1] < ToExpression[str2],
str1 <> " " <> str2, str2 <> " " <> str1]

Out= "157 236"

Of course, you may want to join this code together into a single
function, but it is nice to see the separate steps before creating a
black box!

David Bailey
http://www.dbaileyconsultancy.co.uk

```

• Prev by Date: Re: problems
• Next by Date: Re: Mathematica Collect function
• Previous by thread: Re: Simple String question
• Next by thread: Re: Simple String question