From: S. B. Gray on
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.

Steve Gray


From: Christoph Lhotka on
hi,

I would solve it by writing my own sort function:

sortfoo = (StringJoin @@ # & /@ (Sort /@
IntegerDigits /@ ToExpression /@ StringSplit[#, " "] /.
n_Integer :> ToString[n]) // Sort) /. {x_String, y_String} :>
x <> " " <> y &;

here is a tester function:

With[{foo =
ToString[RandomInteger[{100, 999}]] <> " " <>
ToString[RandomInteger[{100, 999}]]},
{foo -> sortfoo[foo]}]

regards,

christoph

On 02/07/2010 08: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.
>
> Steve Gray
>
>
>



From: Sunt on
On Jul 2, 2:54 pm, "S. B. Gray" <stev...(a)ROADRUNNER.COM> 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.
>
> Steve Gray

Hi,
The following method works, but looks not so simple~~

foo = "623 715";
hf[s_String] := Module[
{sl = StringSplit[s]},
StringJoin@
Insert[StringJoin /@ ((Map[
ToString, #, {2}] &)@(Sort /@ (ToExpression /@ (Characters /
@
sl)))), " ", 2]]
hf[foo]

From: Leonid Shifrin on
Hi Steve,

I don't know how to do that purely with strings, but it is not hard to do by
splitting to characters:

In[1]:=
StringJoin[#1, " ", #2] & @@
StringJoin @@@
Sort[Sort[Characters[#]] & /@ StringSplit["623 715", " "]]

Out[1]= "157 236"

Regards,
Leonid


On Thu, Jul 1, 2010 at 11:54 PM, S. B. Gray <stevebg(a)roadrunner.com> 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.
>
> Steve Gray
>
>
From: Dimitris Emmanoulopoulos on
Dear Steve

This is a potential way to do it

orderingstring[x_] :=
StringReplace[
StringTake[
ToString[
Sort[Map[FromDigits,
ToExpression[
Map[Sort,
StringCases[StringSplit[x], DigitCharacter]]]]]], {2, -2}],
"," -> ""]

orderstring[foo]
157 236

I am pretty sure that "sorter solutions" are going to be posted :-)

Dimitris

 |  Next  |  Last
Pages: 1 2
Prev: boolean variable with contours
Next: problems