« Return to Thread: [vwnc] Bug in MethodDictionary

Re: [vwnc] Bug in MethodDictionary

by Holger Kleinsorgen :: Rate this Message:

Reply to Author | View in Thread

Paul Baumann schrieb:

> Holger wrote:
>>> Maybe it would be useful to have a method similar to #sortedBy: in the
> base image
>
> Here are techniques I use for efficient sorting. Given that...
>
> Sorting usually involves multiple attributes. Sorting needs to be fast.
> Determining all the attributes in advance for each of the objects to be
> compared would not be efficient. A general sorting framework should
> allow for the sorting of dissimilar objects without crashing.
>
> Here is an example of how I sort code models of GemKit:
 > [snip]

we use something similiar in conjunction  with #sortedBy: - a class
called MultiSortKey. It's a subclass of OrderedCollection, and each
element is a sort criteria block.

Example usage (a somewhat constructed example, sorry):

^ self persons sortedBy: [ : person |
     MultiSortKey
        with: person lastName
        with: [ person residence postalCode ]
   ]

the persons are sorted by name first, then by the postal code. in this
example, fetching the postal code is wrapped in a block, because getting
the residence object might require querying a DB. For sorting this will
only be required for persons with the same last name.

The MultiSortKey basically works like this:

MultiSortKey>>= otherCollection

   | size |
   self species == otherCollection species ifFalse: [^false].
   (size := self size) = otherCollection size ifFalse: [^false].
   1 to: size do: [:index |
     (self resolvedAt: index) = (otherCollection resolvedAt: index)
        ifFalse: [^false]].
   ^true


MultiSortKey>>resolvedAt: index
    " lazy evaluation, value is cached "

    | v |
    v := self at: index.
    v kIsBlock
       ifTrue: [
          v := v value.
          self at: index put: v
       ].
    ^ v

_______________________________________________
vwnc mailing list
vwnc@...
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc

 « Return to Thread: [vwnc] Bug in MethodDictionary

LightInTheBox - Buy quality products at wholesale price!