www.smalltalk.org
"The best way to predict the future is to invent it."
Alan Kay
Peter William Lount, OOPSLA 1998, Vancouver, B.C., Canada

Sort Criteria by Peter William Lount peter@smalltalk.org
The Sort Criteria objects extend the standard SortCollection with an easy to use, simple yet powerful and dynamic sorting capability that can sort multiple 'columns' or 'attributes' of a collection of objects. It's easy to rearrange the column sort ordering and the acending or decending state of the sort. It's simple because it just works with SortedCollections as you currently use them. It's powerful because you can sort any sequence of attributes in an object. It's dynamic as a SortCriteria and it's columns can be created on the fly by a user interface enabling the user to choose their own sort order and ascending/decending sequence.

The SortCriteria and SortCriteriaColumn (and SortCriteriaTest for an example) are the classes that are added to the Smalltalk system. No changes to SortedCollection were required or other Smalltalk system classes.


Download SortCriteria_Squeak_v1.st (for Squeak Smalltalk).
Download SortCirteria_ObjectStudio_v1.zip (for ObjectStudio Smalltalk, ported by Salman Zaidi)

Copyright and Usage License
Copyright
SortCriteria and SortCriteriaColumn objects copyright 1996, 1997, 1998, and 1999 by Peter William Lount. All rights reserved. peter@smalltalk.org, http://www.smalltalk.org

Usage License
You may use these objects for any purpose what so ever as long as this notice remains intact. If these objects are used in a commercial software product you must visibly display the above notice.

Contribute
You can contribute by porting the SortCriteria objects to the versions of Smalltalk that you use. I'll post your ports here. Just email me with the file. peter@smalltalk.org

Sort Criteria Test Example
The included SortCriteriaTest object has a class method "test1" that does the following test. It simply creates a SortCriteria object and then configures it's columns, each column being a method to send the objects in the sorted collection. Then the SortedCollection is told to use the new instance of SortCriteria as it's sort block. The results of this test are at the bottom of this test workspace. Change around the order of the columns and their acending or decending sequence as you wish.

With the following Sort Ordering (SortCriteriaTest test1):

	| aSortedList aSortCriteria |
	aSortCriteria := SortCriteria new.
	aSortCriteria addColumnName: #code ascendingFlag: true.
	aSortCriteria addColumnName: #size ascendingFlag: false.
	aSortCriteria addColumnName: #length ascendingFlag: false.

	aSortedList := SortedCollection sortBlock: aSortCriteria.
	self addToList: aSortedList.

    aSortedList printString

The results are (second and third columns are decending). Column Sort ordering is "code, size, and length".

  SortedCollection (
   (SortCriteriaTest code: 'R002' size: '18M' length: 65)
   (SortCriteriaTest code: 'R101' size: '26M' length: 90)
   (SortCriteriaTest code: 'R202' size: '29M' length: 70)
   (SortCriteriaTest code: 'R202' size: '15M' length: 114)
   (SortCriteriaTest code: 'R202' size: '15M' length: 89)
   (SortCriteriaTest code: 'R202' size: '15M' length: 89)
   (SortCriteriaTest code: 'R202' size: '15M' length: 45)
   (SortCriteriaTest code: 'R202' size: '15M' length: 18)
   (SortCriteriaTest code: 'R202' size: '15M' length: 16)
   (SortCriteriaTest code: 'R301' size: '5M'  length: 50)
  )

With a slightly different sort ordering (SortCriteriaTest test2):

	| aSortedList aSortCriteria |
	aSortCriteria := SortCriteria new.
	aSortCriteria addColumnName: #code ascendingFlag: true.
	aSortCriteria addColumnName: #size ascendingFlag: true.
	aSortCriteria addColumnName: #length ascendingFlag: true.

	aSortedList := SortedCollection sortBlock: aSortCriteria.

	self addToList: aSortedList.

        aSortedList printString

The results are (all columns are ascending). Column Sort ordering is "code, size, and length".

  SortedCollection (
   (SortCriteriaTest code: 'R002' size: '18M' length: 65)
   (SortCriteriaTest code: 'R101' size: '26M' length: 90)
   (SortCriteriaTest code: 'R202' size: '15M' length: 16)
   (SortCriteriaTest code: 'R202' size: '15M' length: 18)
   (SortCriteriaTest code: 'R202' size: '15M' length: 45)
   (SortCriteriaTest code: 'R202' size: '15M' length: 89)
   (SortCriteriaTest code: 'R202' size: '15M' length: 89)
   (SortCriteriaTest code: 'R202' size: '15M' length: 114)
   (SortCriteriaTest code: 'R202' size: '29M' length: 70)
   (SortCriteriaTest code: 'R301' size: '5M' length: 50)
 )

With a slightly different sort ordering rearranging the columns so that "length" sorts first (SortCriteriaTest test3).

	| aSortedList aSortCriteria |
	aSortCriteria := SortCriteria new.
	aSortCriteria addColumnName: #length ascendingFlag: false.
	aSortCriteria addColumnName: #code ascendingFlag: true.
	aSortCriteria addColumnName: #size ascendingFlag: true.

	aSortedList := SortedCollection sortBlock: aSortCriteria.

	self addToList: aSortedList.

        aSortedList printString

The results are (first and second columns are ascending, the third column "length" is decending). Column sort sequence is "length, code, and size".

  SortedCollection (
   (SortCriteriaTest code: 'R202' size: '15M' length: 114)
   (SortCriteriaTest code: 'R101' size: '26M' length: 90)
   (SortCriteriaTest code: 'R202' size: '15M' length: 89)
   (SortCriteriaTest code: 'R202' size: '15M' length: 89)
   (SortCriteriaTest code: 'R202' size: '29M' length: 70)
   (SortCriteriaTest code: 'R002' size: '18M' length: 65)
   (SortCriteriaTest code: 'R301' size: '5M' length: 50)
   (SortCriteriaTest code: 'R202' size: '15M' length: 45)
   (SortCriteriaTest code: 'R202' size: '15M' length: 18)
   (SortCriteriaTest code: 'R202' size: '15M' length: 16)
  )

The method that creates the SortCrtieriaTest instances:

addToList: theSortedList

    theSortedList add: (
        SortCriteriaTest new
            code: 'R301';
            size: '5M';
            length: 50;
            yourself
    ).
    theSortedList add: (
        SortCriteriaTest new
            code: 'R202';
            size: '29M';
            length: 70;
            yourself
    ).
    theSortedList add: (
        SortCriteriaTest new
            code: 'R002';
            size: '18M';
            length: 65;
            yourself
    ).
    theSortedList add: (
        SortCriteriaTest new
            code: 'R202';
            size: '15M';
            length: 89;
            yourself
    ).
   theSortedList add: (
        SortCriteriaTest new
            code: 'R101';
            size: '26M';
            length: 90;
            yourself
    ).
    theSortedList add: (
        SortCriteriaTest new
            code: 'R202';
            size: '15M';
            length: 16;
            yourself
    ).
    theSortedList add: (
        SortCriteriaTest new
            code: 'R202';
            size: '15M';
            length: 18;
            yourself
    ).
    theSortedList add: (
        SortCriteriaTest new
            code: 'R202';
            size: '15M';
            length: 45;
            yourself
    ).
    theSortedList add: (
        SortCriteriaTest new
            code: 'R202';
            size: '15M';
            length: 89;
            yourself
    ).
    theSortedList add: (
        SortCriteriaTest new
            code: 'R202';
            size: '15M';
            length: 114;
            yourself
    ).


    ^theSortedList
Home Events FAQs News Groups User Groups Smalltalk Versions Legal Mumbo Jumbo Sponsors Advertising