Sorting Algorithm
Stewart MacLean
stingray at paradise.net.nz
Mon Nov 19 11:11:16 UTC 2001
It's late, can't see it?
>
> The code that Bert gave:
>
> order := [:a :b |
> a title < b title or: [
> a title = b title and: [a name < b name or: [
> a name = b name and: [a ref < b ref or: [
> a number < b number]]]]]]
>
> [There is an obvious error in this, which I leave as an exercise for
> the reader.]
>
Has the essentially the same effect as ...
>
> !BibEntry methodsFor: 'comparing' stamp: 'raok 11/19/2001 14:26'!
> < aBibEntry
> title = aBibEntry title ifFalse: [^title < aBibEntry title].
> name = aBibEntry name ifFalse: [^name < aBibEntry name].
> ref = aBibEntry ref ifFalse: [^ref < aBibEntry ref].
> ^number < aBibEntry number! !
>
or am I too far past my bed time?
Both of these seem cleaner than using the pattern
a title < b title
ifTrue: [true]
ifFalse:
[(a title = b title
and: [a name < b name])
ifTrue: [true]
ifFalse:
[(a title = b title
and: [a name = b name]
and: [a ref < b ref])
ifTrue: [true]
etc
which I usually use.
Cheers,
Stewart
More information about the Squeak-dev
mailing list
|