Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #6655 > unrolled thread
| Started by | Themis Matsoukas <tmatsoukas@me.com> |
|---|---|
| First post | 2011-11-23 12:12 +0000 |
| Last post | 2011-11-24 12:10 +0000 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Sorting strings Themis Matsoukas <tmatsoukas@me.com> - 2011-11-23 12:12 +0000
Re: Sorting strings Costa Bravo <q13a27tt@aol.com> - 2011-11-24 12:07 +0000
Re: Sorting strings "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-11-24 12:07 +0000
Re: Sorting strings Ray Koopman <koopman@sfu.ca> - 2011-11-24 12:10 +0000
| From | Themis Matsoukas <tmatsoukas@me.com> |
|---|---|
| Date | 2011-11-23 12:12 +0000 |
| Subject | Sorting strings |
| Message-ID | <jainvm$eop$1@smc.vnet.net> |
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
[toc] | [next] | [standalone]
| From | Costa Bravo <q13a27tt@aol.com> |
|---|---|
| Date | 2011-11-24 12:07 +0000 |
| Message-ID | <jalc1e$sk4$1@smc.vnet.net> |
| In reply to | #6655 |
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?
>
Lst = {"a1", "a2", "a20", "a12"};
Lst[[Ordering[ToExpression[StringDrop[#, 1] & /@ Lst]]]]
Out = {"a1", "a2", "a12", "a20"}
--
Costa
[toc] | [prev] | [next] | [standalone]
| From | "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> |
|---|---|
| Date | 2011-11-24 12:07 +0000 |
| Message-ID | <jalc2c$sk7$1@smc.vnet.net> |
| In reply to | #6655 |
On Wed, 23 Nov 2011 12:12:38 -0000, Themis Matsoukas <tmatsoukas@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"}
[toc] | [prev] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2011-11-24 12:10 +0000 |
| Message-ID | <jalc75$sm8$1@smc.vnet.net> |
| In reply to | #6655 |
On Nov 23, 4:12 am, Themis Matsoukas <tmatsou...@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"}
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web