Sorting

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Sorting

Chris Cunnington-5
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
Reply | Threaded
Open this post in threaded view
|

Re: Sorting

Edward Stow
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?
>
 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
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Sorting

Bert Freudenberg
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
Reply | Threaded
Open this post in threaded view
|

Re: Sorting

Chris Cunnington-5
Cool. That works beautifully.

Thank you very much,

Chris

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

RE: Sorting

Mark Bailey-5
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
Reply | Threaded
Open this post in threaded view
|

Re: Sorting

Scott Wallace-2
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
Reply | Threaded
Open this post in threaded view
|

Re: Sorting

Chris Cunnington-5

> 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