comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: sorting the chars in a string
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: sorting the chars in a string [message #58634 is a reply to message #58633] Mon, 11 February 2008 07:02 Go to previous message
lasse is currently offline  lasse
Messages: 48
Registered: February 2007
Member
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: sorting the chars in a string
Next Topic: the problem of making the example of externel calltest

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Fri Oct 10 20:30:39 PDT 2025

Total time taken to generate the page: 1.28197 seconds