[Newbies] Sorting
Edward Stow
ed.stow at gmail.com
Wed May 16 17:53:16 UTC 2007
On 17/05/07, Chris Cunnington <cunnington at sympatico.ca> wrote:
> Hi Squeakers,
>
> I have a string: 'Hello'.
>
> To reverse the letters I write:
>
> a := 'Hello' reverse.
>
> And I get 'olleH'.
>
> How do I sort the letters alphabetically? So I get 'eHllo'?
>
> a := 'Hello' sortBy: [:x :y | a < b ]
>
> What parameter do you use to sort the letters? Do you have to convert them
> to a number, an Ascii or Unicode number first, and then sort those numbers?
>
a := 'Hello' sort: [:x :y | x < y ]
or
a := 'Hello' sort: [:a :b | a < b ] but convention is to use x and
y block parameters.
Each element of the string is a Character and has the #< defined.
Note that 'Hello' is already sorted in Character order.
Others may like to comment on why
#sortBy: does not return a string.
--
Edward Stow
More information about the Beginners
mailing list