What is wrong with my sort: block?

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

What is wrong with my sort: block?

Rick Flower-2
Ok.. First time using sort and I'm stumped..

I've got a collection of associations that look
kinda like this :

     [1]: './foo'->true
     [2]: './bar'->false
     [3]: './baz/->false

I want to sort them so the entry in the collection
with the 'true' association value is at the end --
so after sorting it should look like :

     [1]: './bar'->false
     [2]: './baz/->false
     [3]: './foo'->true

But when I execute something like that shown
below it always returns the same thing -- namely
no change..  I even tried hard-coding ^false when
it was only 2 entries and it made no difference..
Any ideas?

myCollection sort: [:a :b |
    a value ifTrue: [^false ].
    ^true.
].
myCollection inspect



_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: What is wrong with my sort: block?

Ladislav Marek
You are using non-local return in block, thats not correct (in this
case). ^false will return from your method and sort: will not be
completed. Last expression is the result in block, so correct code is:

myCollection sort: [ :a :b | a value not ].
myCollection inspect.

On Tue, Dec 11, 2012 at 3:28 AM, Rick Flower <[hidden email]> wrote:

> Ok.. First time using sort and I'm stumped..
>
> I've got a collection of associations that look
> kinda like this :
>
>     [1]: './foo'->true
>     [2]: './bar'->false
>     [3]: './baz/->false
>
> I want to sort them so the entry in the collection
> with the 'true' association value is at the end --
> so after sorting it should look like :
>
>     [1]: './bar'->false
>     [2]: './baz/->false
>     [3]: './foo'->true
>
> But when I execute something like that shown
> below it always returns the same thing -- namely
> no change..  I even tried hard-coding ^false when
> it was only 2 entries and it made no difference..
> Any ideas?
>
> myCollection sort: [:a :b |
>    a value ifTrue: [^false ].
>    ^true.
> ].
> myCollection inspect
>
>
>
> _______________________________________________
> help-smalltalk mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-smalltalk

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: What is wrong with my sort: block?

Holger Freyther
In reply to this post by Rick Flower-2
On Mon, Dec 10, 2012 at 06:28:58PM -0800, Rick Flower wrote:
Hi,


> But when I execute something like that shown
> below it always returns the same thing -- namely
> no change..  I even tried hard-coding ^false when
> it was only 2 entries and it made no difference..
> Any ideas?
>
> myCollection sort: [:a :b |
>    a value ifTrue: [^false ].
>    ^true.
> ].
> myCollection inspect

two guesses
a.) A copy is returned by your invocation of sort.
b.) You should really compare a to b? Return only
true if a is true and b is false?

holger


_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: What is wrong with my sort: block?

Rick Flower-2
In reply to this post by Ladislav Marek
On 11.12.2012 00:12, Ladislav Marek wrote:

> You are using non-local return in block, thats not correct (in this
> case). ^false will return from your method and sort: will not be
> completed. Last expression is the result in block, so correct code
> is:
>
> myCollection sort: [ :a :b | a value not ].
> myCollection inspect.

BINGO!  Worked like a charm.. So, what you're saying is that the
forced ^false will essentially abort the sort operation completely
leaving it unchanged?  Ok.. Did some googling around for the
phrase "non-local return smalltalk" and found a good definition
on the objectmix.com website.. I'd never heard of this before but it
certainly explains the behavior I was seeing.. Thanks for the heads-up!



_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk