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? Cheers, Chris Cunnington _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
On 17/05/07, Chris Cunnington <[hidden email]> 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? > 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 _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by Chris Cunnington-5
On May 16, 2007, at 19:10 , Chris Cunnington 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? 'Hello' sort: [:a :b | a asUppercase < b asUppercase] - Bert - _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Cool. That works beautifully.
Thank you very much, Chris _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by Edward Stow
<< #sortBy: does not return a string. >>
I was surprised that the collection returned does not respond to a message like asString. Anyway, this short code works: t _ ('The cow jumped over the moon' sortBy: [ :x :y | x < y ]). s _ String new: t size. 1 to: t size do: [ :i | s at: i put: (t at: i) ]. You can also apply this approach based on inject:into: t _ ('The cow jumped over the moon' sortBy: [ :x :y | x < y ]). t inject: String new into: [ :x :y | x, (String with: y) ]. Mark _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Try ('The cow jumped over the mon' sortBy: [:x :y | x < y]) as: String.
On May 17, 2007, at 5:24 AM, Mark Bailey wrote: > << #sortBy: does not return a string. >> > > I was surprised that the collection returned does not respond to a > message like asString. Anyway, this short code works: > > t _ ('The cow jumped over the moon' sortBy: [ :x :y | x < y ]). > s _ String new: t size. > 1 to: t size do: [ :i | s at: i put: (t at: i) ]. > > You can also apply this approach based on inject:into: > > t _ ('The cow jumped over the moon' sortBy: [ :x :y | x < y ]). > t inject: String new into: [ :x :y | x, (String with: y) ]. > > Mark > _______________________________________________ > Beginners mailing list > [hidden email] > http://lists.squeakfoundation.org/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
> Try ('The cow jumped over the mon' sortBy: [:x :y | x < y]) as: String. That one's pretty cool. Sometimes I've tried things and you get a nasty array of characters ($a $b $c). That's an unpleasant answer. This solution puts it back into the form I expected initially. 'abc' Groovy. Chris _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |