How to sort a SortedCollection like you can sort a table in Excel?

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

How to sort a SortedCollection like you can sort a table in Excel?

Paul DeBruicker
Hi -


If I had a SortedCollection of rectangular solid objects (that just have
the ivars height, width, depth), how could I sort it by height, then
width, then depth?

So three object:

A) h: 10, w: 5, d: 3
B) h: 60, w: 2, d: 14
C) h: 10, w: 3, d: 27

And then after sorting they'd be in this order:

B) h: 60, w: 2, d: 14
A) h: 10, w: 5, d: 3
C) h: 10, w: 3, d: 27

First sorted by height, then by width, then by depth.  

Is there a way to do that using just a #sortBlock:  

My only other approach would be to make a Set of the heights, sort that,
then #select: a collection for each height, sort that by weight, then
for each height make a Set of weights etc...
 

Thanks

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

Re: How to sort a SortedCollection like you can sort a table in Excel?

Bert Freudenberg
On 12.04.2010, at 16:13, Paul DeBruicker wrote:

>
> Hi -
>
>
> If I had a SortedCollection of rectangular solid objects (that just have
> the ivars height, width, depth), how could I sort it by height, then
> width, then depth?
>
> So three object:
>
> A) h: 10, w: 5, d: 3
> B) h: 60, w: 2, d: 14
> C) h: 10, w: 3, d: 27
>
> And then after sorting they'd be in this order:
>
> B) h: 60, w: 2, d: 14
> A) h: 10, w: 5, d: 3
> C) h: 10, w: 3, d: 27
>
> First sorted by height, then by width, then by depth.  
>
> Is there a way to do that using just a #sortBlock:  
>

Sure. Basically your sort block would look like

        a height > b height or: [
                a height = b height and: [
                        a width > b width or: [
                                a width = b width and: [
                                        a depth > b depth]]]]


- Bert -


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

Re: How to sort a SortedCollection like you can sort a table in Excel?

Bert Freudenberg
On 12.04.2010, at 16:36, Bert Freudenberg wrote:

>
> On 12.04.2010, at 16:13, Paul DeBruicker wrote:
>>
>> Hi -
>>
>>
>> If I had a SortedCollection of rectangular solid objects (that just have
>> the ivars height, width, depth), how could I sort it by height, then
>> width, then depth?
>>
>> So three object:
>>
>> A) h: 10, w: 5, d: 3
>> B) h: 60, w: 2, d: 14
>> C) h: 10, w: 3, d: 27
>>
>> And then after sorting they'd be in this order:
>>
>> B) h: 60, w: 2, d: 14
>> A) h: 10, w: 5, d: 3
>> C) h: 10, w: 3, d: 27
>>
>> First sorted by height, then by width, then by depth.  
>>
>> Is there a way to do that using just a #sortBlock:  
>>
>
> Sure. Basically your sort block would look like
>
> a height > b height or: [
> a height = b height and: [
> a width > b width or: [
> a width = b width and: [
> a depth > b depth]]]]
>
>
> - Bert -

Hmm, indenting it like this makes it more obvious:

        a height > b height or: [
        a height = b height and: [
                a width > b width or: [
                a width = b width and: [
                        a depth > b depth]]]]

- Bert -


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

Re: How to sort a SortedCollection like you can sort a table in Excel?

Andreas.Raab
On 4/12/2010 7:48 AM, Bert Freudenberg wrote:
> Hmm, indenting it like this makes it more obvious:
>
> a height>  b height or: [
> a height = b height and: [
> a width>  b width or: [
> a width = b width and: [
> a depth>  b depth]]]]

Personally, I write multi-level sort blocks like here:

        a height = b height
                ifTrue:[a width = b width
                        ifTrue:[a depth >= b depth]]
                        ifFalse:[a width >= b width]
                ifFalse:[a height >= b height]

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

Re: How to sort a SortedCollection like you can sort a table in Excel?

keith1y
I add a <= to

then you can write:

  [ :a :b | { a width. a depth. } <= { b width. b depth } ]

regards

Keith





On 12 Apr 2010, at 17:52, Andreas Raab wrote:

> On 4/12/2010 7:48 AM, Bert Freudenberg wrote:
>> Hmm, indenting it like this makes it more obvious:
>>
>> a height>  b height or: [
>> a height = b height and: [
>> a width>  b width or: [
>> a width = b width and: [
>> a depth>  b depth]]]]
>
> Personally, I write multi-level sort blocks like here:
>
> a height = b height
> ifTrue:[a width = b width
> ifTrue:[a depth >= b depth]]
> ifFalse:[a width >= b width]
> ifFalse:[a height >= b height]
>
> Cheers,
>  - Andreas
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners

0009-SequencableCollectionLessThan.1.st (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to sort a SortedCollection like you can sort a table in Excel?

Jerome Peace
In reply to this post by Bert Freudenberg
Hi Bert, Hi DeBruicker
a slight tweak to Bert's method

a height = b height
 ifFalse: [ a height < b height ]
  ifTrue: [ a width = b width
    ifFalse: [ a width < b width ]
    ifTrue: [ a depth <= b depth ] ] .

Basically if primary sorts values are unequal sort by primary values else sort by secondry values etc. This basicly recusive procedure will work for any number of sort levels.

Yours in curiosity and service, --Jerome Peace




--- On Mon, 4/12/10, Bert Freudenberg <[hidden email]> wrote:

> From: Bert Freudenberg <[hidden email]>
> Subject: Re: [Newbies] How to sort a SortedCollection like you can sort a table in Excel?
> To: "A friendly place to get answers to even the most basic questions about Squeak." <[hidden email]>
> Date: Monday, April 12, 2010, 10:36 AM
> On 12.04.2010, at 16:13, Paul
> DeBruicker wrote:
> >
> > Hi -
> >
> >
> > If I had a SortedCollection of rectangular solid
> objects (that just have
> > the ivars height, width, depth), how could I sort it
> by height, then
> > width, then depth?
> >
> > So three object:
> >
> > A) h: 10, w: 5, d: 3
> > B) h: 60, w: 2, d: 14
> > C) h: 10, w: 3, d: 27
> >
> > And then after sorting they'd be in this order:
> >
> > B) h: 60, w: 2, d: 14
> > A) h: 10, w: 5, d: 3
> > C) h: 10, w: 3, d: 27
> >
> > First sorted by height, then by width, then by
> depth. 
> >
> > Is there a way to do that using just a
> #sortBlock: 
> >
>
> Sure. Basically your sort block would look like
>
>     a height > b height or: [
>         a height = b height
> and: [
>             a
> width > b width or: [
>            
>     a width = b width and: [
>            
>         a depth > b
> depth]]]]
>
>
> - Bert -
>
>
> _______________________________________________
> 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: How to sort a SortedCollection like you can sort a table in Excel?

Jerome Peace
In reply to this post by keith1y


--- On Mon, 4/12/10, keith <[hidden email]> wrote:

> From: keith <[hidden email]>
> Subject: Re: [Newbies] Re: How to sort a SortedCollection like you can sort a table in Excel?
> To: "A friendly place to get answers to even the most basic questions about Squeak." <[hidden email]>
> Date: Monday, April 12, 2010, 4:53 PM
> I add a <= to
>
> then you can write:
>
>  [ :a :b | { a width. a depth. } <= { b width. b depth }
> ]
>
> regards
>
> Keith

For the win!

Cool, Keith.

It's easy to forget how smart collection are.

Cheers, --Jer


>
>
> On 12 Apr 2010, at 17:52, Andreas Raab wrote:
>
> > On 4/12/2010 7:48 AM, Bert Freudenberg wrote:
> >> Hmm, indenting it like this makes it more
> obvious:
> >>
> >>     a height>  b height or:
> [
> >>     a height = b height and: [
> >>         a
> width>  b width or: [
> >>         a width = b
> width and: [
> >>        
>     a depth>  b depth]]]]
> >
> > Personally, I write multi-level sort blocks like
> here:
> >
> >     a height = b height
> >         ifTrue:[a width
> = b width
> >        
>     ifTrue:[a depth >= b depth]]
> >        
>     ifFalse:[a width >= b width]
> >         ifFalse:[a
> height >= b height]
> >
> > Cheers,
> >  - Andreas
> > _______________________________________________
> > Beginners mailing list
> > [hidden email]
> > http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
>
> -----Inline Attachment Follows-----
>
> _______________________________________________
> 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: How to sort a SortedCollection like you can sort a table in Excel?

keith1y
In reply to this post by Jerome Peace

On 13 Apr 2010, at 01:03, Jerome Peace wrote:

> Hi Bert, Hi DeBruicker
> a slight tweak to Bert's method
>
> a height = b height
> ifFalse: [ a height < b height ]
>  ifTrue: [ a width = b width
>    ifFalse: [ a width < b width ]
>    ifTrue: [ a depth <= b depth ] ] .
>
> Basically if primary sorts values are unequal sort by primary values  
> else sort by secondry values etc. This basicly recusive procedure  
> will work for any number of sort levels.
>
> Yours in curiosity and service, --Jerome Peace
>


Hi Jerome,

If you look at my implementation you will probably hate it! For  
practical purposes, in my <= implementation for SequencableCollection  
I also handles nil <= anything else, and false <= true.

cheers

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

Re: How to sort a SortedCollection like you can sort a table in Excel?

Jerome Peace
HI Keith,

I had thought you were pointing to something already implemented in collection. If it were it is an nice elegant language form.

I don't have an opinion yet about your generalized implementation. In what context do you use it?

In any event your form would not be useful in all contexts. The one Andreas and I use works better when you want to cut off a comparison before evaluating the secondary or tertiary messages.

In the context of the stated problem it would probably work fine.

Squeak has a deficiency. It can't express properly the concept of plurals.
Ideally I should have message selectors that could work with one or more of a certain type of object. With sqeuak you can't quite get thar from heyah.


Anyway you showed an interesting alternative.

Cheers --Jer  





--- On Tue, 4/13/10, keith <[hidden email]> wrote:

> From: keith <[hidden email]>
> Subject: Re: [Newbies] How to sort a SortedCollection like you can sort a table in Excel?
> To: "A friendly place to get answers to even the most basic questions about Squeak." <[hidden email]>
> Date: Tuesday, April 13, 2010, 9:11 AM
>
> On 13 Apr 2010, at 01:03, Jerome Peace wrote:
>
> > Hi Bert, Hi DeBruicker
> > a slight tweak to Bert's method
> >
> > a height = b height
> > ifFalse: [ a height < b height ]
> >  ifTrue: [ a width = b width
> >    ifFalse: [ a width < b width ]
> >    ifTrue: [ a depth <= b depth ] ] .
> >
> > Basically if primary sorts values are unequal sort by
> primary values else sort by secondry values etc. This
> basicly recusive procedure will work for any number of sort
> levels.
> >
> > Yours in curiosity and service, --Jerome Peace
> >
>
>
> Hi Jerome,
>
> If you look at my implementation you will probably hate it!
> For practical purposes, in my <= implementation for
> SequencableCollection I also handles nil <= anything
> else, and false <= true.
>
> cheers
>
> Keith
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners