Sorting help!

Bert Freudenberg bert at isg.cs.uni-magdeburg.de
Thu Nov 15 14:44:33 UTC 2001


On Thu, 15 Nov 2001 goran.hultgren at bluefish.se wrote:

> Sort would have to do in ascending order of
> <title>,<name>,<reference>,<number>
> 
> this is to say:
> 
> IF <title1>=<title2> THEN
> order by <name>: IF <name1>=<name2> THEN
> order by <ref>: IF <ref1>=<ref2> THEN
> order by <number>

You can use any sorting algorithm you like. The only thing you need to
define is an order on your objects. That is, you need a function that
computes if object A sorts before object B. And this is easy:

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]]]]]]

Then you can use a SortedCollection with this order to sort:

  s := SortedCollection sortBlock: order.
  s addAll: unsorted.
  s do: [:obj | obj print]

Hope this helps. That code is not tested. No warranty etc. pp.

-- Bert





More information about the Squeak-dev mailing list