Re: sorting the chars in a string [message #58633 is a reply to message #58632] |
Mon, 11 February 2008 07:36   |
elwood
Messages: 23 Registered: February 2007
|
Junior Member |
|
|
Excellent!
Thank you!
A minor variation on the below-
what if I want to split a string into a string array in which each
element is one character?
I think I can figure out a lengthy way to do it if I first convert the
string to byte.
BUT
strjoin will knit a string array back together, but is there a way to
use strsplit to rip off each char,
one by one and put them into a string array?
Something like the following perhaps;
x="abdcf"
arrayofchars=strsplit(x,'.',/regex,/extract)
-the above doesnt work, it returns an empty string :-(
On Feb 11, 9:02 am, Lasse Clausen <la...@lbnc.de> wrote:
> On 11 Feb, 15:39, elwood <epolo...@uwsp.edu> wrote:
>
>> I've been trying to think of a simple way to alphabetically sort an
>> individual string
>
>> Say I have the string x="abdcf"
>> Is there a way to sort it and return a new string which is in correct
>> alphabetical order,
>> and/or reverse alphabetical order.
>
>> Maybe its obvious, but I havent worked with strings too much in IDL
>> and I cant seem to find
>> a way to do this (strsplit and stregex somehow??)
>
>> Thanks,
>> Elisha
>
> Hi there,
>
> convert the string to an array of bytes, like so
>
> test = 'sfbvvaedfvtrgvsdvrbnyhtfc'
> tmp = byte(test)
>
> This conversion happens according to the ASCII table, if I am not
> mistanken, which connects a number to a character in ascending order,
> on my machine a = 97, b = 98 and so on. Then sort that byte array and
> build up the string again.
>
> sort_inds = sort(tmp)
> new_string = string(tmp[sort_inds])
> print, new_string
>
> That works, use REVERSE() if you want the sorting done from z-a.
>
> Cheers
> Lasse Clausen
|
|
|